Homepage Demos Overview Downloads Tutorials Reference
Credits

SphereData.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <vector>
00003 #include <list>
00004 #include <math.h>
00005 
00006 #include "Motion/Kinematics.h"  // for kine
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); // *** DST hack
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 //! Print information about this shape. (Virtual in BaseData.)
00057 void SphereData::printParams() const {
00058   cout << "Type = " << getTypeName();
00059   cout << "Shape ID = " << getId() << endl;
00060   cout << "Parent ID = " << getParentId() << endl;
00061   
00062   // Print critical points.
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 //! Transformations. (Virtual in BaseData.)
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()); // pick a tangent point from cam point.
00090   Point cam_pt(cam_pos(1)/cam_pos(4), cam_pos(2)/cam_pos(4), cam_pos(3)/cam_pos(4)); // position of camera w.r.t. base
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); // tangent line from camera to sphere
00104   LineData cam_center(getSpace(), cam_pt, centroid); // line from camera passing through center point of sphere
00105 
00106   // a line perpendicular to tangent_line should cross cam_center line at the center point of the sphere if it
00107   // crosses tangent_line at the tangent point. Distance b/w tangent point and center point is the radius of sphere
00108   // which should also equal the height of the sphere (coordZ = 1/groundplane(3) + radius)
00109   // line from tangent_pt to centroid: z = ax + b (a known, b unkown)
00110   // line from camera to centroid: z = cx + d (c,d known)
00111   // tangent_line: z = ex + f (e,f known)
00112   // tangent_pt: x = (f-b)/(a-e)
00113   // centroid: x = (d-b)/(a-c), z = d + c(d-b)/(a-c) = 1/groundplane(3) + radius = (radius above groud level)
00114   // solve for b and substitute it to get centroid and radius
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; // perpendicular to 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])); // radius = dx * DXtoR where dx is b/w center pt and tangent pt
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 // BEGIN SKETCH MANIPULATION AND LINE EXTRACTION CODE
00154 // ==================================================
00155 
00156 
00157 //! Extraction.
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   //! Declare all colors as valid.
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   // Loop through all valid colors.
00200   for(cur_color = 0; cur_color < num_colors; cur_color++) {
00201     
00202     if(Valid_Colors[cur_color] == true) {
00203       
00204       // Segment color pixels.
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       // Extract spheres.
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 //! Render into a sketch space and return reference. (Private.)
00227 Sketch<bool>* SphereData::render() const {
00228   const int cx = int(centerPt().getCoords()(1));
00229   const int cy = int(centerPt().getCoords()(2));
00230   /*  
00231   // Sure the sphere rendering is terribly inefficient, but it works
00232   float a = getRadius();
00233   float x_skip = atan(1/(0.5*a)); // minimum x-diff w/o gaps 
00234   for( float x = (cx-a); x<(cx+a); x+=x_skip) {
00235     float y_y0_sq = 1 - (x-cx)*(x-cx);
00236     if(y_y0_sq > 0) {
00237       int y_bot = cy + (int)(sqrt(y_y0_sq));
00238       int y_top = cy - (int)(sqrt(y_y0_sq));
00239       draw_result((int)x,y_bot) = true;
00240       draw_result((int)x,y_top) = true;
00241     }
00242   }
00243   draw_result(cx-(int)a,cy) = true; // fill in "holes" at ends
00244   draw_result(cx+(int)a,cy) = true;
00245   */  
00246   // Fill the sphere.
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; // rad/10 added to make sphere look nicer
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 } // namespace

DualCoding 3.0beta
Generated Wed Oct 4 00:01:54 2006 by Doxygen 1.4.7