00001
00002
00003 #include <iostream>
00004 #include <vector>
00005
00006 #include "BaseData.h"
00007 #include "Point.h"
00008 #include "ShapeTypes.h"
00009
00010 #include "SketchSpace.h"
00011 #include "Sketch.h"
00012 #include "visops.h"
00013
00014 #include "ShapeSpace.h"
00015 #include "ShapeRoot.h"
00016
00017 #include "PointData.h"
00018 #include "ShapePoint.h"
00019
00020 namespace DualCoding {
00021
00022 PointData::PointData(ShapeSpace& _space, const Point &c)
00023 : BaseData(_space,getStaticType()), the_point(c) {
00024 the_point.refFrameType = space->getRefFrameType();
00025 }
00026
00027 DATASTUFF_CC(PointData);
00028
00029 bool PointData::isMatchFor(const ShapeRoot& other) const {
00030 if (!(isSameTypeAs(other) && isSameColorAs(other)))
00031 return false;
00032 const Shape<PointData>& other_point = ShapeRootTypeConst(other,PointData);
00033 float dist = the_point.distanceFrom(other_point->getCentroid());
00034 return dist < 20;
00035 }
00036
00037 void PointData::mergeWith(const ShapeRoot& other) {
00038 const Shape<PointData>& other_point = ShapeRootTypeConst(other,PointData);
00039 if (other_point->confidence <= 0)
00040 return;
00041 const int other_conf = other_point->confidence;
00042 confidence += other_conf;
00043 the_point = (the_point*confidence + other_point->getCentroid()*other_conf) / (confidence+other_conf);
00044 }
00045
00046 bool PointData::updateParams(const ShapeRoot& other, bool) {
00047 const Shape<PointData>& other_point = *static_cast<const Shape<PointData>*>(&other);
00048 ++confidence;
00049 the_point = (the_point*(confidence-1) + other_point->getCentroid())/confidence;
00050 deleteRendering();
00051 return true;
00052 }
00053
00054
00055 void
00056 PointData::printParams() const {
00057 cout << "Type = " << getTypeName();
00058 cout << "Shape ID = " << getId() << endl;
00059 cout << "Parent ID = " << getParentId() << endl;
00060
00061
00062 cout << endl;
00063 cout << "center{" << getCentroid().coordX() << ", " << getCentroid().coordY() << "}" << endl;
00064 printf("color = %d %d %d\n",getColor().red,getColor().green,getColor().blue);
00065 cout << "viewable = " << isViewable() << endl;
00066 }
00067
00068
00069
00070 void PointData::applyTransform(const NEWMAT::Matrix& Tmat) {
00071 the_point.applyTransform(Tmat);
00072 }
00073
00074 void PointData::projectToGround(const NEWMAT::Matrix& camToBase,
00075 const NEWMAT::ColumnVector& groundplane) {
00076 the_point.projectToGround(camToBase,groundplane);
00077 }
00078
00079
00080
00081
00082
00083
00084
00085
00086 std::vector<ShapeRoot> PointData::extractPoints(const Sketch<bool>& sketch)
00087 {
00088 SketchSpace &SkS = sketch->getSpace();
00089 ShapeSpace &ShS = SkS.getDualSpace();
00090 std::vector<ShapeRoot> result;
00091 size_t num_pixels = sketch.width * sketch.height;
00092 for ( size_t i=0; i < num_pixels; i++ )
00093 if ( sketch[i] ) {
00094 int const y = int(i/sketch.width);
00095 int const x = i - y*sketch.width;
00096 Point pt(x,y,0);
00097 SkS.applyTmat(pt.getCoords());
00098 Shape<PointData> new_point_shape(new PointData(ShS,pt));
00099 new_point_shape->inheritFrom(sketch.rootGetData());
00100 result.push_back(new_point_shape);
00101 }
00102 return result;
00103 }
00104
00105
00106
00107 Sketch<bool>* PointData::render() const {
00108 SketchSpace &SkS = space->getDualSpace();
00109 NEWMAT::ColumnVector ctr(getCentroid().getCoords());
00110 SkS.applyTmat(ctr);
00111 int const cx = int(ctr(1));
00112 int const cy = int(ctr(2));
00113 Sketch<bool>& draw_result =
00114 *new Sketch<bool>(SkS, "render("+getName()+")");
00115 draw_result = false;
00116 draw_result(cx,cy) = true;
00117 return &draw_result;
00118 }
00119
00120 PointData& PointData::operator=(const PointData& other) {
00121 if (&other == this)
00122 return *this;
00123 BaseData::operator=(other);
00124 the_point = other.the_point;
00125 return *this;
00126 }
00127
00128 }