void HexaGridMap::onTouch(cocos2d::Touch *touch, cocos2d::Event *event, int DownOrMove) {
    //把螢幕的觸控點轉成六角座標
    auto pts = Utils::calPointWithPosition(touch->getLocation().x,
                                           touch->getLocation().y,
                                           this->mapOrigin,
                                           this->ptsMargin,
                                           this->rotateRad);
    //校正用,為了解BUG
    int inRegionCnt = 0;
    Point inRegionPt;
    for (int i = 0; i < 4; i++) {
        Point testPt((int)pts.x + (i%2) - (pts.x < 0), (int)pts.y + ((i/2)%2) - (pts.y < 0));
        if (this->getChildByName(Utils::generateNameByPoint(testPt))) {
            auto testUnit = this->getUnitWithPos(testPt);
            auto touchVec = Utils::rotate(touch->getLocation()-this->mapOrigin, -this->rotateRad);
            if (testUnit->getBoundingBox().containsPoint(touchVec)) {
                inRegionCnt++;
                inRegionPt = testPt;
            }
            if (inRegionCnt == 2)
                break;
        }
    }
    
    //找到方塊了,抓到trajactory上面
    if (inRegionCnt == 1 && this->getChildByName(Utils::generateNameByPoint(inRegionPt))) {
        if (DownOrMove == TOUCH_DOWN && this->trajectory.size() == 0) {
            this->getUnitWithPos(inRegionPt)->selected();
            this->trajectory.push_back(inRegionPt);
        } else if ( (DownOrMove == TOUCH_DOWN || DownOrMove == TOUCH_MOVE) && this->trajectory.size() > 0) {
            if (isTrajPointLegal(inRegionPt)) {
                //偵測有沒有畫回來
                if (this->trajectory.size() > 1 && this->trajectory[this->trajectory.size()-2] == inRegionPt) {
                    auto name = Utils::generateNameByPoint(this->trajectory.back());
                    this->getChildByName<HexaGridMapUnit*>(name)->unselected();
                    this->trajectory.pop_back();
                } else {
                    auto name = Utils::generateNameByPoint(inRegionPt);
                    //can replaced by getUnitWithPos()
                    if (!this->getChildByName<HexaGridMapUnit*>(name)->isSelected()) {
                        this->getChildByName<HexaGridMapUnit*>(name)->selected();
                        this->trajectory.push_back(inRegionPt);
                    }
                }
                this->updateTrajectory();
            }
        }
    }
}
Example #2
0
/**
Try to find a point that lies within (or on) the object
@param[out] point :: on exit set to the point value, if found
@return 1 if point found, 0 otherwise
*/
int MeshObject::getPointInObject(Kernel::V3D &point) const {

  Kernel::V3D testPt(0, 0, 0);
  // Try centre of bounding box as initial guess, if we have one.
  const BoundingBox &boundingBox = getBoundingBox();
  if (boundingBox.isNonNull()) {
    testPt = boundingBox.centrePoint();
    if (searchForObject(testPt)) {
      point = testPt;
      return 1;
    }
  }

  return 0;
}
Example #3
0
bool DecalRoad::containsPoint( const Point3F &worldPos, U32 *nodeIdx ) const
{
   // This is just for making selections in the editor, we use the 
   // client-side road because it has the proper edge's.
   if ( isServerObject() && getClientObject() )
      return ((DecalRoad*)getClientObject())->containsPoint( worldPos, nodeIdx );

   // If point isn't in the world box, 
   // it's definitely not in the road.
   //if ( !getWorldBox().isContained( worldPos ) )
   //   return false;

   if ( mEdges.size() < 2 )
      return false;

   Point2F testPt(   worldPos.x, 
                     worldPos.y );
   Point2F poly[4];

   // Look through all edges, does the polygon
   // formed from adjacent edge's contain the worldPos?
   for ( U32 i = 0; i < mEdges.size() - 1; i++ )
   {
      const RoadEdge &edge0 = mEdges[i];
      const RoadEdge &edge1 = mEdges[i+1];

      poly[0].set( edge0.p0.x, edge0.p0.y );
      poly[1].set( edge0.p2.x, edge0.p2.y );
      poly[2].set( edge1.p2.x, edge1.p2.y );
      poly[3].set( edge1.p0.x, edge1.p0.y );

      if ( MathUtils::pointInPolygon( poly, 4, testPt ) )
      {
         if ( nodeIdx )
            *nodeIdx = edge0.parentNodeIdx;

         return true;
      }

   }

   return false;
}
Example #4
0
int main( )
{
    Algebraic_real_1 real( 1 );
    //CGAL::Qt::Converter< Algebraic_kernel_d_2 > testConverter;
    //CGAL::Qt::Converter< CKvA_2 > testConverter;
    
    //CGAL::Qt::Converter< Cartesian > testConverter;
    Kernel_point_2 testPt( 1, 2 );
    Point_2 testPt2( testPt.x( ), testPt.y( ) );
    Traits traits;
    Construct_curve_2 constructCurve = traits.construct_curve_2_object( );
    Curve_2 curve = constructCurve( makeParabola( ) );
    Make_x_monotone_2 mm = traits.make_x_monotone_2_object( );
    std::vector< CGAL::Object > curves;
    mm( curve, std::back_inserter( curves ) );
    std::cout << curves.size( ) << std::endl;
    X_monotone_curve_2 c1;
    CGAL::assign( c1, curves[ 0 ] );
    double lb = -3;
    double ub = 3;
    double step = 6.0 / 1000;

    for ( int i = 0; i < 1000; ++i )
    {
        X_monotone_curve_2 c2 = makeVerticalLine( lb + step * i );

        CGAL::Object o;
        CGAL::Oneset_iterator< CGAL::Object > oi( o );
        Intersect_2 intersect = traits.intersect_2_object( );
        intersect( c1, c2, oi );
        std::pair< Point_2, Multiplicity > res;
        CGAL::assign( res, o );
        std::pair< double, double > approx = res.first.to_double( );
        std::cout << approx.first << " " << approx.second << std::endl;
    }


    return 0;
}
Example #5
0
bool DecalRoad::containsPoint( const Point3F &worldPos, U32 *nodeIdx ) const
{
   // If point isn't in the world box, 
   // it's definitely not in the road.
   //if ( !getWorldBox().isContained( worldPos ) )
   //   return false;

   if ( mEdges.size() < 2 )
      return false;

   Point2F testPt(   worldPos.x, 
                     worldPos.y );
   Point2F poly[4];

   // Look through all edges, does the polygon
   // formed from adjacent edge's contain the worldPos?
   for ( U32 i = 0; i < mEdges.size() - 1; i++ )
   {
      const RoadEdge &edge0 = mEdges[i];
      const RoadEdge &edge1 = mEdges[i+1];

      poly[0].set( edge0.p0.x, edge0.p0.y );
      poly[1].set( edge0.p2.x, edge0.p2.y );
      poly[2].set( edge1.p2.x, edge1.p2.y );
      poly[3].set( edge1.p0.x, edge1.p0.y );

      if ( MathUtils::pointInPolygon( poly, 4, testPt ) )
      {
         if ( nodeIdx )
            *nodeIdx = edge0.parentNodeIdx;

         return true;
      }

   }

   return false;
}
Example #6
0
bool ossimViewshedUtil::initializeChain()
{
   if (m_observerGpt.hasNans())
   {
      ossimNotify(ossimNotifyLevel_WARN)
                  << "ossimViewshedUtil::initialize ERR: Observer ground position has not been set."
                  << std::endl;
      return false;
   }

   ossimElevManager* elevMgr = ossimElevManager::instance();

   // If DEM provided as file on command line, reset the elev manager to use only this:
   if (!m_demFile.empty())
   {
      elevMgr->clear();
      ossimRefPtr<ossimImageElevationDatabase> ied = new ossimImageElevationDatabase;
      if(!ied->open(m_demFile))
      {
         ossimNotify(ossimNotifyLevel_WARN)
                     << "ossimViewshedUtil::initialize ERR: Cannot open DEM file at <"<<m_demFile<<">\n"
                     << std::endl;
         return false;
      }

      if (m_simulation)
         ied->setGeoid(new ossimIdentityGeoid);

      elevMgr->addDatabase(ied.get());

      // Possibly the image size has not been specified, in which case we use the same dimensions
      // as the input dem:
      if (((m_halfWindow == 0) && (m_visRadius == 0)) || (m_gsd == 0))
      {

         ossimRefPtr<ossimImageHandler> dem = ossimImageHandlerRegistry::instance()->open(m_demFile);
         if (!dem.valid())
         {
            ossimNotify(ossimNotifyLevel_WARN)
                        << "ossimViewshedUtil::initialize ERR: Cannot open DEM file at <"<<m_demFile<<">\n"
                        << std::endl;
            return false;
         }
         ossimRefPtr<ossimImageGeometry> geom = dem->getImageGeometry();
         if (!geom.valid())
         {
            ossimNotify(ossimNotifyLevel_WARN)
                        << "ossimViewshedUtil::initialize ERR: Could not establish geometry of DEM file at <"<<m_demFile<<">\n"
                        << std::endl;
            return false;
         }

         // Hack workaround for ossimElevManager::getMeanSpacingMeters() returning 0 when DEM file
         // specified:
         if (m_gsd == 0)
         {
            ossimDpt gsd = geom->getMetersPerPixel();
            m_gsd = (gsd.x + gsd.y)/2.0;
         }
         if ((m_halfWindow == 0) && (m_visRadius == 0))
         {
            ossimIpt size = geom->getImageSize();
            m_halfWindow = (size.x + size.y) / 4;
         }
      }

      // When DEM file specified, need to turn off all defaulting to ellipsoid/geoid to make sure
      // only the DEM file data is processed:
      elevMgr->setDefaultHeightAboveEllipsoid(ossim::nan());
      elevMgr->setUseGeoidIfNullFlag(false);
   }

   if (m_simulation)
      elevMgr->setEnableFlag(false);

   // Initialize the height of eye component of observer position:
   m_observerGpt.hgt = ossimElevManager::instance()->getHeightAboveEllipsoid(m_observerGpt);
   m_observerGpt.hgt += m_obsHgtAbvTer;

   // Determine if default GSD needs to be computed.
   if (m_gsd == 0)
   {
      // This is incorrectly returning 0 when DEM is provided on command line:
      m_gsd = ossimElevManager::instance()->getMeanSpacingMeters();
      if (ossim::isnan(m_gsd))
         m_gsd = 0;
   }

   // Compute the bounding rect in pixel space given the visibility range and the GSD:
   if ((m_gsd == 0) || ((m_visRadius == 0) && (m_halfWindow == 0)))
   {
      ossimNotify(ossimNotifyLevel_WARN)
                  << "ossimViewshedUtil::initialize ERR: GSD, visibility radius or image size have not"
                  " been set."  << std::endl;
      return false;
   }
   if (m_halfWindow == 0)
      m_halfWindow = ossim::round<ossim_int32, double>(m_visRadius/m_gsd);

   m_viewRect.set_ulx(-m_halfWindow);
   m_viewRect.set_uly(-m_halfWindow);
   m_viewRect.set_lrx(m_halfWindow);
   m_viewRect.set_lry(m_halfWindow);
   ossimIpt image_size (m_viewRect.width(), m_viewRect.height());

   // Establish the image geometry's map projection:
   ossimRefPtr<ossimEquDistCylProjection> mapProj = new ossimEquDistCylProjection();
   mapProj->setOrigin(m_observerGpt);
   mapProj->setMetersPerPixel(ossimDpt(m_gsd, m_gsd));
   ossimDpt degPerPixel (mapProj->getDecimalDegreesPerPixel());
   mapProj->setElevationLookupFlag(true);
   ossimGpt ulTiePt (m_observerGpt);
   ulTiePt.lat += degPerPixel.lat * m_halfWindow;
   ulTiePt.lon -= degPerPixel.lon * m_halfWindow;
   mapProj->setUlTiePoints(ulTiePt);

   // Need a transform so that we can use the observer point as the output image origin (0,0):
   ossimRefPtr<ossim2dTo2dTransform> transform =  new ossim2dTo2dShiftTransform(m_viewRect.lr());
   m_geometry = new ossimImageGeometry(transform.get(), mapProj.get());
   m_geometry->setImageSize(image_size);

   // Allocate the output image buffer:
   m_outBuffer = ossimImageDataFactory::instance()->create(0, OSSIM_UINT8, 1,
                                                           m_viewRect.width(), m_viewRect.height());
   if(!m_outBuffer.valid())
      return false;

   // Initialize the image with all points hidden:
   m_outBuffer->initialize();
   m_outBuffer->setImageRectangle(m_viewRect);
   m_outBuffer->fill(m_visibleValue);

#if 0
   //### TODO: REMOVE DEBUG BLOCK
   {
      ossimDpt viewPt;
      m_geometry->worldToLocal(m_observerGpt, viewPt);
      cout<<"ossimViewshedUtil::initialize() should get (0,0)... viewPt="<<viewPt<<endl;
      ossimGpt testPt(m_observerGpt);
      testPt.lat -= 100*degPerPixel.y;
      testPt.lon += 100*degPerPixel.x;
      m_geometry->worldToLocal(testPt, viewPt);
      cout<<"ossimViewshedUtil::initialize() should get ~(100,100)... viewPt="<<viewPt<<endl;
   }
#endif

   // Initialize the radials:
   initRadials();

   if (m_outputSummary)
      dumpProductSummary();

   m_initialized = true;
   return true;
}
/**
 *	This method returns a value idicating how much this cloud rule
 *	correlates to the passed in weather settings.  The returned
 *	value is not normalised, but can be compared to any other
 *	correlation value.  A value close to 0 indicates greater correlation.
 */
float Clouds::Rule::correlation( const WeatherSettings& ws ) const
{
	Vector3 testPt( ws.colourMin, ws.cover, ws.cohesion );
	testPt -= position_;
	return testPt.lengthSquared();
}