00001 #include <iostream>
00002 #include <vector>
00003 #include <list>
00004 #include <math.h>
00005
00006 #include "Motion/Kinematics.h"
00007
00008 #include "SketchSpace.h"
00009 #include "Sketch.h"
00010 #include "ShapeRoot.h"
00011 #include "LineData.h"
00012 #include "Region.h"
00013 #include "visops.h"
00014
00015 #include "SphereData.h"
00016 #include "ShapeSphere.h"
00017
00018 namespace DualCoding {
00019
00020 SphereData::SphereData(ShapeSpace& _space, const Point &c)
00021 : BaseData(_space,sphereDataType),
00022 centroid(c), radius(0)
00023 { mobile = SPHERE_DATA_MOBILE; }
00024
00025 SphereData::SphereData(const SphereData& otherData)
00026 : BaseData(otherData),centroid(otherData.centroid),radius(otherData.radius)
00027 { mobile = otherData.mobile; }
00028
00029 DATASTUFF_CC(SphereData);
00030
00031 bool SphereData::isMatchFor(const ShapeRoot& other) const {
00032 if (!(isSameTypeAs(other) && isSameColorAs(other)))
00033 return false;
00034 const Shape<SphereData>& other_sphere = ShapeRootTypeConst(other,SphereData);
00035 float dist = centroid.distanceFrom(other_sphere->centerPt());
00036 return dist < 2*max(radius,other_sphere->radius);
00037 }
00038
00039 void SphereData::mergeWith(const ShapeRoot& other) {
00040 const Shape<SphereData>& other_sphere = ShapeRootTypeConst(other,SphereData);
00041 if (other_sphere->confidence <= 0)
00042 return;
00043 const int other_conf = other_sphere->confidence;
00044 confidence += other_conf;
00045 centroid = (centroid*confidence + other_sphere->centerPt()*other_conf) / (confidence+other_conf);
00046 radius = (radius*confidence + other_sphere->getRadius()*other_conf) / (confidence+other_conf);
00047 }
00048
00049 bool SphereData::updateParams(const ShapeRoot& other, bool) {
00050 const Shape<SphereData>& other_sphere = *static_cast<const Shape<SphereData>*>(&other);
00051 centroid = (centroid*(confidence-1) + other_sphere->getCentroid())/confidence;
00052 radius = (radius*(confidence-1) + other_sphere->getRadius())/confidence;
00053 return true;
00054 }
00055
00056
00057 void SphereData::printParams() const {
00058 cout << "Type = " << getTypeName();
00059 cout << "Shape ID = " << getId() << endl;
00060 cout << "Parent ID = " << getParentId() << endl;
00061
00062
00063 cout << endl;
00064 cout << "center{" << centerPt().coordX() << ", " << centerPt().coordY() << "}" << endl;
00065
00066 cout << "radius = " << getRadius() << endl;
00067 printf("color = %d %d %d\n",getColor().red,getColor().green,getColor().blue);
00068 cout << "mobile = " << isMobile() << endl;
00069 cout << "viewable = " << isViewable() << endl;
00070 }
00071
00072
00073
00074 void SphereData::applyTransform(const NEWMAT::Matrix& Tmat) {
00075 centroid.applyTransform(Tmat);
00076 }
00077
00078 bool SphereData::isInside(const Point& pt) const {
00079 float dist = pt.distanceFrom(centerPt());
00080 return radius>dist;
00081 }
00082
00083
00084 void SphereData::projectToGround(const NEWMAT::Matrix& camToBase,
00085 const NEWMAT::ColumnVector& groundplane) {
00086 NEWMAT::ColumnVector cam_pos = (kine->jointToBase(CameraFrameOffset)).SubMatrix(1,4,4,4);
00087 cout << "cam position (" << (cam_pos(1)/cam_pos(4)) << ","
00088 << (cam_pos(2)/cam_pos(4)) << "," << (cam_pos(3)/cam_pos(4)) << ")" << endl;
00089 Point tangent_pt(centroid.coordX(),centroid.coordY()+radius, centroid.coordZ());
00090 Point cam_pt(cam_pos(1)/cam_pos(4), cam_pos(2)/cam_pos(4), cam_pos(3)/cam_pos(4));
00091 cout << "sphere in cam frame: centroid:" << "(" << centroid.coordX()
00092 << "," << centroid.coordY() << "," << centroid.coordZ() << "); tangent_pt:"
00093 << "(" << tangent_pt.coordX() << "," << tangent_pt.coordY() << "," << tangent_pt.coordZ()
00094 << ")" << endl;
00095
00096 centroid.projectToGround(camToBase,groundplane);
00097 tangent_pt.projectToGround(camToBase,groundplane);
00098 cout << "sphere projected to ground: centroid:" << "(" << centroid.coordX()
00099 << "," << centroid.coordY() << "," << centroid.coordZ() << "); tangent_pt:"
00100 << "(" << tangent_pt.coordX() << "," << tangent_pt.coordY() << "," << tangent_pt.coordZ()
00101 << ")" << endl;
00102
00103 LineData tangent_line(getSpace(), cam_pt, tangent_pt);
00104 LineData cam_center(getSpace(), cam_pt, centroid);
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 vector<float> t_abc_xz = tangent_line.lineEquation_abc_xz();
00117 vector<float> cc_abc_xz = cam_center.lineEquation_abc_xz();
00118 vector<float> cc_abc_xy = cam_center.lineEquation_abc();
00119
00120 const float f = cc_abc_xz[2] / cc_abc_xz[1];
00121 const float e = - cc_abc_xz[0] / cc_abc_xz[1];
00122 const float d = t_abc_xz[2] / t_abc_xz[1];
00123 const float c = - t_abc_xz[0] / t_abc_xz[1];
00124 const float a = -1.0 / e;
00125 const float ground = 1.0/groundplane(3);
00126 const float DXtoR = 1/ cos(atan(a)) / cos(atan(-cc_abc_xy[0]/cc_abc_xy[1]));
00127 const float b = (-DXtoR*f*a+DXtoR*f*c+DXtoR*d*a-DXtoR*d*e+ground*a*a-ground*a*c-ground*e*a+ground*e*c-d*a*a+d*e*a)/(-a*c+e*c+DXtoR*c-DXtoR*e);
00128
00129 cout << "ground level: " << ground << ", DXtoR: " << DXtoR << endl;
00130 cout << "tangent line: z = " << e << " * x + " << f << endl;
00131 cout << "perpendicular line: z = " << a << " * x + " << b << endl;
00132 cout << "center line: z = " << c << " * x + " << d << endl;
00133 cout << "dx b/w tangent pt and center pt: " << ((f-b)/(a-e)-(d-b)/(a-c)) << endl;
00134
00135 const float x = (d-b)/(a-c);
00136 const float z = d + c*(d-b)/(a-c);
00137 const float y = (cc_abc_xy[2]-cc_abc_xy[0]*x) / cc_abc_xy[1];
00138
00139 centroid.setCoords(x,y,z);
00140 radius = z-ground;
00141
00142 cout << " => (" << x << "," << y << "," << z << "); radius: " << radius << endl;
00143 }
00144
00145 void SphereData::setRadius(float _radius) {
00146 radius = _radius;
00147 deleteRendering();
00148 }
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 std::vector<ShapeRoot> SphereData::extractSpheres(const Sketch<bool>& sketch)
00160 {
00161 const float AREA_TOLERANCE = 0.5;
00162 const int REGION_THRESH = 25;
00163 NEW_SKETCH_N(labels,usint,visops::oldlabelcc(sketch,visops::EightWayConnect));
00164 list<Region> regionlist = Region::extractRegions(labels,REGION_THRESH);
00165 std::vector<ShapeRoot> spheres;
00166
00167 if(regionlist.empty())
00168 return spheres;
00169
00170 typedef list<Region>::iterator R_IT;
00171 for (R_IT it = regionlist.begin(); it != regionlist.end(); ++it) {
00172 float ratio = it->findSemiMajorAxisLength()/(float)(it->findSemiMinorAxisLength());
00173 if((ratio < 2.0) && (ratio > 1.0/(float)2.0)
00174 && (it->findArea() > M_PI*2.0*(it->findSemiMajorAxisLength())
00175 *2.0*(it->findSemiMinorAxisLength())*AREA_TOLERANCE/4.0)) {
00176 Shape<SphereData> temp_sphere(*it);
00177 temp_sphere->setParentId(sketch->getViewableId());
00178 temp_sphere->setColor(sketch->getColor());
00179 spheres.push_back(Shape<SphereData>(temp_sphere));
00180 };
00181 }
00182 return spheres;
00183 }
00184
00185 std::vector<ShapeRoot> SphereData::get_spheres(const Sketch<uchar>& cam) {
00186
00187 std::vector<bool> Valid_Colors;
00188 Valid_Colors.resize(ProjectInterface::getNumColors(),true);
00189 return(get_spheres(cam,Valid_Colors));
00190 }
00191
00192 std::vector<ShapeRoot> SphereData::get_spheres(const Sketch<uchar>& cam,
00193 std::vector<bool>& Valid_Colors) {
00194 std::vector<ShapeRoot> spheres_vec;
00195 uchar cur_color;
00196 uchar num_colors = (uchar)Valid_Colors.size();
00197 char *pmask_name_chr = (char *)malloc(128*sizeof(char));
00198
00199
00200 for(cur_color = 0; cur_color < num_colors; cur_color++) {
00201
00202 if(Valid_Colors[cur_color] == true) {
00203
00204
00205 NEW_SKETCH_N(pmask, bool, cam == cur_color);
00206 sprintf(pmask_name_chr, "pmask_%d",cur_color);
00207 pmask->setName(pmask_name_chr);
00208
00209
00210 std::vector<ShapeRoot> spheresList = SphereData::extractSpheres(pmask);
00211
00212 int num_spheres = (int)spheresList.size();
00213 int cur_sphere;
00214
00215 for(cur_sphere = 0; cur_sphere < num_spheres; cur_sphere++) {
00216 spheresList[cur_sphere]->setColor(ProjectInterface::getColorRGB(cur_color));
00217 spheres_vec.push_back(spheresList[cur_sphere]);
00218 }
00219
00220 };
00221 }
00222 return(spheres_vec);
00223 }
00224
00225
00226
00227 Sketch<bool>* SphereData::render() const {
00228 const int cx = int(centerPt().getCoords()(1));
00229 const int cy = int(centerPt().getCoords()(2));
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248 Sketch<bool> result(space->getDualSpace(), "render("+getName()+")");
00249 result = 0;
00250 const int rad =(int) floor(getRadius()+0.5);
00251 const int radSq = rad*rad + rad/10;
00252 const int minX = (rad > cx) ? 0 : cx-rad;
00253 const int maxX = ((unsigned int) (rad+cx) > getSpace().getDualSpace().getWidth()-1)
00254 ? getSpace().getDualSpace().getWidth()-1 : cx+rad;
00255 for (int x = minX; x <= maxX; x++) {
00256 const int yRange = (int) sqrt((float) (radSq-(cx-x)*(cx-x)));
00257 const int minY = (yRange > cy) ? 0 : cy-yRange;
00258 const int maxY = ((unsigned int) yRange+cy > getSpace().getDualSpace().getHeight()-1)
00259 ? getSpace().getDualSpace().getHeight()-1 : cy+yRange;
00260 for (int y = minY; y <= maxY; y++)
00261 result(x,y) = true;
00262 }
00263 return new Sketch<bool>(result);
00264 }
00265
00266
00267 }