bool operator()(const Point2D & p1, const Point2D & p2) { double cp = doubleSignedTriangleArea(referencePoint, p1, p2); if(fabs(cp) < EPS) { return squareDistance(referencePoint, p1) < squareDistance(referencePoint, p2); } return cp > 0.0; }
int ConnectionTool::handleAtPoint(KoShape *shape, const QPointF &mousePoint) const { if (!shape) return -1; const QPointF shapePoint = shape->documentToShape(mousePoint); KoConnectionShape * connectionShape = dynamic_cast<KoConnectionShape*>(shape); if (connectionShape) { // check connection shape handles return connectionShape->handleIdAt(handleGrabRect(shapePoint)); } else { // check connection points int grabDistance = grabSensitivity(); qreal minDistance = HUGE_VAL; int handleId = -1; KoConnectionPoints connectionPoints = shape->connectionPoints(); KoConnectionPoints::const_iterator cp = connectionPoints.constBegin(); KoConnectionPoints::const_iterator lastCp = connectionPoints.constEnd(); for(; cp != lastCp; ++cp) { qreal d = squareDistance(shapePoint, cp.value().position); if (d <= grabDistance && d < minDistance) { handleId = cp.key(); minDistance = d; } } return handleId; } }
Point<Dim> KDTree<Dim>::findNearestNeighbor(const Point<Dim> & query, int lo, int hi, int dim) const{ if(lo >= hi ) return points[lo]; //base case int mid = (hi+lo)/2; //calculate the minPoint index Point<Dim> temp; Point<Dim> curr = points[mid]; dim = dim%Dim; int direction=0; if(smallerDimVal(query, points[mid], dim)){ direction = 0; temp = findNearestNeighbor(query, lo, mid-1, dim+1); //left half } else{ direction =1; temp = findNearestNeighbor(query, mid+1, hi, dim+1); //recusively call the function on the left half } Point<Dim> best; if(shouldReplace(query, temp, curr) ) best= curr; else best = temp; int dimD = (curr[dim] - query[dim])*(curr[dim] - query[dim]); int bestCurr = squareDistance(best, query); if(dimD > bestCurr) return best; else{ Point<Dim> t; if(direction == 0) t = findNearestNeighbor(query, mid+1, hi, dim+1); else t = findNearestNeighbor(query, lo, mid-1,dim+1); if(shouldReplace(query,t, best)) return best; else return t; } }
bool KDTree<Dim>::shouldReplace(const Point<Dim> & target, const Point<Dim> & currentBest, const Point<Dim> & potential) const { int squareSumCurr=0; int squareSumPot=0; /*for(int i = 0; i< Dim; i++){ squareSumCurr += (target[i] - currentBest[i])*(target[i] - currentBest[i]); squareSumPot += (target[i] - potential[i])*(target[i] - potential[i]); }*/ squareSumCurr = squareDistance(target, currentBest); squareSumPot = squareDistance(target, potential); //if they are tie, based on operator < , in this case is false if(squareSumCurr == squareSumPot) return potential < currentBest; return squareSumPot < squareSumCurr; /** * @todo Implement this function! */ }
void TerrainHeightHandler::NoiseTerrainHeight() { if(!m_bValidate) return ; if(!WorldEditorRenderInterface::GetInstance()->IsIntersectTerrain()) return; float x,z; int i,j; ISGPWorldSystemManager* pWorldManager=WorldEditorRenderInterface::GetInstance()->GetWorldSystemManager(); WorldEditorRenderInterface::GetInstance()->GetIntersectPoint(x,z); int chunkcount=WorldEditorRenderInterface::GetInstance()->GetChunkSize(); float maxRange=(float)chunkcount*SGPTT_TILENUM*SGPTT_TILE_METER; int min_intx = jlimit(0,chunkcount*SGPTT_TILENUM,(int)((x-m_OuterRadius)/SGPTT_TILE_METER-1)); int max_intx = jlimit(0,chunkcount*SGPTT_TILENUM,(int)((x+m_OuterRadius)/SGPTT_TILE_METER+1)); int min_intz = jlimit(0,chunkcount*SGPTT_TILENUM,(int)((maxRange-z-m_OuterRadius)/SGPTT_TILE_METER-1)); int max_intz = jlimit(0,chunkcount*SGPTT_TILENUM,(int)((maxRange-z+m_OuterRadius)/SGPTT_TILE_METER+1)); float squareRadius=m_OuterRadius*m_OuterRadius; int randRange=(10-2)*(m_Intensity-1)/(100-1)+2; for(i=min_intx;i<=max_intx;++i) { for(j=min_intz;j<=max_intz;++j) { float posx=(float)i*SGPTT_TILE_METER; float posz=maxRange-(float)j*SGPTT_TILE_METER; if(squareDistance(x,z,posx,posz)<=squareRadius) { TerrainHeightOpData opData; opData.index_x=i; opData.index_z=j; int dHeight=rand()%randRange; int prevHeight,currHeight; if(!m_pVertexHeightOp1->FindData(opData,prevHeight,currHeight)) { prevHeight=WorldEditorRenderInterface::GetInstance()->GetTerrainHeight(i,j); currHeight=prevHeight; } opData.prevHeight=prevHeight; opData.currHeight=jlimit(0,65535,currHeight+dHeight); pWorldManager->setHeightMap(opData.index_x,opData.index_z,opData.currHeight); if(opData.prevHeight!=opData.currHeight) { m_pVertexHeightOp1->AddData(opData); // Add chunk info HeightOp1AddChunk(i,j); } } } } m_pVertexHeightOp1->FlushTerrainHeight(); }
void ConnectionTool::mouseReleaseEvent(KoPointerEvent *event) { if (m_currentStrategy) { if (m_editMode == CreateConnection) { // check if connection handles have a minimal distance KoConnectionShape * connectionShape = dynamic_cast<KoConnectionShape*>(m_currentShape); Q_ASSERT(connectionShape); // get both handle positions in document coordinates QPointF p1 = connectionShape->shapeToDocument(connectionShape->handlePosition(0)); QPointF p2 = connectionShape->shapeToDocument(connectionShape->handlePosition(1)); int grabDistance = grabSensitivity(); // use grabbing sensitivity as minimal distance threshold if (squareDistance(p1, p2) < grabDistance*grabDistance) { // minimal distance was not reached, so we have to undo the started work: // - cleanup and delete the strategy // - remove connection shape from shape manager and delete it // - reset edit mode to last state delete m_currentStrategy; m_currentStrategy = 0; repaintDecorations(); canvas()->shapeManager()->remove(m_currentShape); setEditMode(m_editMode, connectionShape->firstShape(), connectionShape->firstConnectionId()); repaintDecorations(); delete connectionShape; return; } else { // finalize adding the new connection shape with an undo command KUndo2Command * cmd = canvas()->shapeController()->addShape(m_currentShape); canvas()->addCommand(cmd); setEditMode(EditConnection, m_currentShape, KoConnectionShape::StartHandle); } } m_currentStrategy->finishInteraction(event->modifiers()); // TODO: Add parent command to KoInteractionStrategy::createCommand // so that we can have a single command to undo for the user KUndo2Command *command = m_currentStrategy->createCommand(); if (command) canvas()->addCommand(command); delete m_currentStrategy; m_currentStrategy = 0; } updateStatusText(); }
// updateOldTouch - called for all hidd events that are not finger down // this updates state and position of the touches and returns true if a new touch event has to be sent bool QPAHiddTpHandler::updateOldTouch(QPAHiddTpHandler::HiddTouch& touch) { bool updated = false; QList<HiddTouch>::iterator it; // finding the old touch to update for (it = m_touches.begin(); it != m_touches.end(); ++it) { if (it->hiddId == touch.hiddId) break; } if (it == m_touches.end()) { g_debug ("did not find existing touch to update, ignoring the touch"); return false; } it->seenInScan = true; it->time = touch.time; if (touch.state == FingerUp) { it->state = touch.state; updated = true; } else if (squareDistance((*it), touch) > 0) { it->state = FingerMove; it->x = touch.x; it->y = touch.y; updated = true; } else { it->state = FingerNoMove; } // updates do not modify id as this is assigned in addNewTouch // updates do not modify isPrimary as this is marked in addNewTouch and removeReleasedTouches /* if (it->isMetaTouch && (it->state == FingerUp || it->y <= m_deviceHeight)) { it->isMetaTouch = false; m_metaActiveTouchesCount--; } */ return updated; }
void TerrainHeightHandler::SmoothTerrainHeight() { if(!m_bValidate) return ; if(!WorldEditorRenderInterface::GetInstance()->IsIntersectTerrain()) return; float x,z; int i,j; ISGPWorldSystemManager* pWorldManager= WorldEditorRenderInterface::GetInstance()->GetWorldSystemManager(); WorldEditorRenderInterface::GetInstance()->GetIntersectPoint(x,z); int chunkcount=WorldEditorRenderInterface::GetInstance()->GetChunkSize(); int pointCount=chunkcount*SGPTT_TILENUM; float maxRange=(float)chunkcount*SGPTT_TILENUM*SGPTT_TILE_METER; int min_intx = jlimit(0,pointCount,(int)((x-m_OuterRadius)/SGPTT_TILE_METER-1)); int max_intx = jlimit(0,pointCount,(int)((x+m_OuterRadius)/SGPTT_TILE_METER+1)); int min_intz = jlimit(0,pointCount,(int)((maxRange-z-m_OuterRadius)/SGPTT_TILE_METER-1)); int max_intz = jlimit(0,pointCount,(int)((maxRange-z+m_OuterRadius)/SGPTT_TILE_METER+1)); float squareRadius=m_OuterRadius*m_OuterRadius; for(i=min_intx;i<=max_intx;++i) { for(j=min_intz;j<=max_intz;++j) { float posx=(float)i*SGPTT_TILE_METER; float posz=maxRange-(float)j*SGPTT_TILE_METER; if(squareDistance(x,z,posx,posz)<=squareRadius) { TerrainHeightOpData opData; opData.index_x=i; opData.index_z=j; int prevHeight,currHeight; if(!m_pVertexHeightOp1->FindData(opData,prevHeight,currHeight)) { prevHeight=WorldEditorRenderInterface::GetInstance()->GetTerrainHeight(i,j); } int count=1; int height=WorldEditorRenderInterface::GetInstance()->GetTerrainHeight(i,j); if(j>0) { height+=WorldEditorRenderInterface::GetInstance()->GetTerrainHeight(i,j-1); ++count; } if(i<pointCount&&j>0) { height+=WorldEditorRenderInterface::GetInstance()->GetTerrainHeight(i+1,j-1); ++count; } if(i>0) { height+=WorldEditorRenderInterface::GetInstance()->GetTerrainHeight(i-1,j); ++count; } if(i>0&&j>0) { height+=WorldEditorRenderInterface::GetInstance()->GetTerrainHeight(i-1,j-1); ++count; } if(i<pointCount) { height+=WorldEditorRenderInterface::GetInstance()->GetTerrainHeight(i+1,j); ++count; } if(i>0&&j<pointCount) { height+=WorldEditorRenderInterface::GetInstance()->GetTerrainHeight(i-1,j+1); ++count; } if(j<pointCount) { height+=WorldEditorRenderInterface::GetInstance()->GetTerrainHeight(i,j+1); ++count; } if(i<pointCount&&j<pointCount) { height+=WorldEditorRenderInterface::GetInstance()->GetTerrainHeight(i+1,j+1); ++count; } opData.prevHeight=prevHeight; opData.currHeight=jlimit(0,65535,height/count); pWorldManager->setHeightMap(opData.index_x,opData.index_z,opData.currHeight); if(opData.prevHeight!=opData.currHeight) { m_pVertexHeightOp1->AddData(opData); // Add chunk info HeightOp1AddChunk(i,j); } } } } m_pVertexHeightOp1->FlushTerrainHeight(); }
void TerrainHeightHandler::ReduceTerrainHeight() { if(!m_bValidate) return ; if(!WorldEditorRenderInterface::GetInstance()->IsIntersectTerrain()) return; float x,z; int i,j; WorldEditorRenderInterface::GetInstance()->GetIntersectPoint(x,z); ISGPWorldSystemManager* pWorldManager = WorldEditorRenderInterface::GetInstance()->GetWorldSystemManager(); int chunkcount=WorldEditorRenderInterface::GetInstance()->GetChunkSize(); float maxRange=(float)chunkcount*SGPTT_TILENUM*SGPTT_TILE_METER; // In Inner Circle int min_intx = jlimit(0,chunkcount*SGPTT_TILENUM,(int)((x-m_InnerRadius)/SGPTT_TILE_METER-1)); int max_intx = jlimit(0,chunkcount*SGPTT_TILENUM,(int)((x+m_InnerRadius)/SGPTT_TILE_METER+1)); int min_intz = jlimit(0,chunkcount*SGPTT_TILENUM,(int)((maxRange-z-m_InnerRadius)/SGPTT_TILE_METER-1)); int max_intz = jlimit(0,chunkcount*SGPTT_TILENUM,(int)((maxRange-z+m_InnerRadius)/SGPTT_TILE_METER+1)); int strength=(8-1)*(m_Intensity-1)/(100-1)+1; float squareInnerRadius = m_InnerRadius*m_InnerRadius; for(i=min_intx;i<=max_intx;++i) { for(j=min_intz;j<=max_intz;++j) { float posx=(float)i*SGPTT_TILE_METER; float posz=maxRange-(float)j*SGPTT_TILE_METER; if(squareDistance(x,z,posx,posz)<=squareInnerRadius) { TerrainHeightOpData opData; opData.index_x=i; opData.index_z=j; int prevHeight,currHeight; if(!m_pVertexHeightOp1->FindData(opData,prevHeight,currHeight)) { prevHeight=WorldEditorRenderInterface::GetInstance()->GetTerrainHeight(i,j); currHeight=prevHeight; } opData.prevHeight=prevHeight; opData.currHeight=jlimit(0,65535,currHeight-strength); pWorldManager->setHeightMap(opData.index_x,opData.index_z,opData.currHeight); if(opData.currHeight!=opData.prevHeight) { m_pVertexHeightOp1->AddData(opData); // Add chunk info HeightOp1AddChunk(i,j); } } } } // In Outer Circle min_intx = jlimit(0,chunkcount*SGPTT_TILENUM,(int)((x-m_OuterRadius)/SGPTT_TILE_METER-1)); max_intx = jlimit(0,chunkcount*SGPTT_TILENUM,(int)((x+m_OuterRadius)/SGPTT_TILE_METER+1)); min_intz = jlimit(0,chunkcount*SGPTT_TILENUM,(int)((maxRange-z-m_OuterRadius)/SGPTT_TILE_METER-1)); max_intz = jlimit(0,chunkcount*SGPTT_TILENUM,(int)((maxRange-z+m_OuterRadius)/SGPTT_TILE_METER+1)); for(i=min_intx;i<=max_intx;++i) { for(j=min_intz;j<=max_intz;++j) { float posx=(float)i*SGPTT_TILE_METER; float posz=maxRange-(float)j*SGPTT_TILE_METER; float distance=sqrt(squareDistance(x,z,posx,posz)); if(distance<=m_OuterRadius&&distance>m_InnerRadius) { TerrainHeightOpData opData; opData.index_x=i; opData.index_z=j; int prevHeight,currHeight; if(!m_pVertexHeightOp1->FindData(opData,prevHeight,currHeight)) { prevHeight=WorldEditorRenderInterface::GetInstance()->GetTerrainHeight(i,j); currHeight=prevHeight; } opData.prevHeight=prevHeight; float ratio=(m_OuterRadius-distance)/(m_OuterRadius-m_InnerRadius); opData.currHeight=jlimit(0,65535,currHeight-(int)(ratio*strength)); pWorldManager->setHeightMap(opData.index_x,opData.index_z,opData.currHeight); if(opData.prevHeight!=opData.currHeight) { m_pVertexHeightOp1->AddData(opData); // Add chunk info HeightOp1AddChunk(i,j); } } } } m_pVertexHeightOp1->FlushTerrainHeight(); }
void TerrainHeightHandler::RetrieveAdjustVertex() { if(!m_bValidate) return ; float x,z; int i,j; WorldEditorRenderInterface::GetInstance()->GetIntersectPoint(x,z); int chunkcount=WorldEditorRenderInterface::GetInstance()->GetChunkSize(); float maxRange=(float)chunkcount*SGPTT_TILENUM*SGPTT_TILE_METER; // Inner Circle int min_intx = jlimit(0,chunkcount*SGPTT_TILENUM,(int)((x-m_InnerRadius)/SGPTT_TILE_METER-1)); int max_intx = jlimit(0,chunkcount*SGPTT_TILENUM,(int)((x+m_InnerRadius)/SGPTT_TILE_METER+1)); int min_intz = jlimit(0,chunkcount*SGPTT_TILENUM,(int)((maxRange-z-m_InnerRadius)/SGPTT_TILE_METER-1)); int max_intz = jlimit(0,chunkcount*SGPTT_TILENUM,(int)((maxRange-z+m_InnerRadius)/SGPTT_TILE_METER+1)); float squareInnerRadius = m_InnerRadius*m_InnerRadius; for(i=min_intx;i<=max_intx;++i) { for(j=min_intz;j<=max_intz;++j) { float posx=(float)i*SGPTT_TILE_METER; float posz=maxRange-(float)j*SGPTT_TILE_METER; if(squareDistance(x,z,posx,posz) <= squareInnerRadius) { AdjustedPoint adjustedPoint; adjustedPoint.index_x=i; adjustedPoint.index_z=j; adjustedPoint.ratio=1.0f; m_AdjustPointVector.push_back(adjustedPoint); TerrainHeightOpData opData; opData.index_x=i; opData.index_z=j; int height=WorldEditorRenderInterface::GetInstance()->GetTerrainHeight(i,j); opData.prevHeight=opData.currHeight=height; m_pVertexHeightOp2->AddData(opData); // Add chunk info HeightOp2AddChunk(i,j); } } } if(m_OuterRadius-m_InnerRadius <= 0.02f) return ; /******************************In Outer Circle******************************/ min_intx = jlimit(0,chunkcount*SGPTT_TILENUM,(int)((x-m_OuterRadius)/SGPTT_TILE_METER-1)); max_intx = jlimit(0,chunkcount*SGPTT_TILENUM,(int)((x+m_OuterRadius)/SGPTT_TILE_METER+1)); min_intz = jlimit(0,chunkcount*SGPTT_TILENUM,(int)((maxRange-z-m_OuterRadius)/SGPTT_TILE_METER-1)); max_intz = jlimit(0,chunkcount*SGPTT_TILENUM,(int)((maxRange-z+m_OuterRadius)/SGPTT_TILE_METER+1)); float squareOuterRadius = m_OuterRadius * m_OuterRadius; for(i=min_intx;i<=max_intx;++i) { for(j=min_intz;j<=max_intz;++j) { float posx=(float)i*SGPTT_TILE_METER; float posz=maxRange-(float)j*SGPTT_TILE_METER; float distance = (float)sqrt((x-posx)*(x-posx)+(z-posz)*(z-posz)); if(distance<=m_OuterRadius&&distance>m_InnerRadius) { AdjustedPoint adjustPoint; adjustPoint.index_x=i; adjustPoint.index_z=j; float ratio=(m_OuterRadius-distance)/(m_OuterRadius-m_InnerRadius); adjustPoint.ratio=ratio; m_AdjustPointVector.push_back(adjustPoint); TerrainHeightOpData opData; opData.index_x=i; opData.index_z=j; int height=WorldEditorRenderInterface::GetInstance()->GetTerrainHeight(i,j); opData.prevHeight=opData.currHeight=height; m_pVertexHeightOp2->AddData(opData); // Add chunk info HeightOp2AddChunk(i,j); } } } }
//this method is deprecated in 006 please use squareDistance float ofxPoint2f::distanceSquared( const ofxPoint2f& pnt ) const { return squareDistance(pnt); }