bool QModelIndexComparator::operator()(const QModelIndex& lhs, const QModelIndex& rhs) const { const std::vector<QModelIndex> lhs_hierarchy = getHierarchyOf(lhs); const std::vector<QModelIndex> rhs_hierarchy = getHierarchyOf(rhs); const std::size_t s = std::min(lhs_hierarchy.size(), rhs_hierarchy.size()); bool result = false; for (std::size_t i = 0; i < s; i++) { const QModelIndex& lhs_item = lhs_hierarchy[i]; const QModelIndex& rhs_item = rhs_hierarchy[i]; if (isLess(lhs_item, rhs_item)) { result = true; break; } if (isLess(rhs_item, lhs_item)) { result = false; break; } } return result; }
unsigned *cannon_detect_collisions(CANNON *c, OBJECT_POSITION *asteroids, unsigned asteroids_size) { unsigned i, j, k, aNext, removedI; float minX, maxX, minY, maxY; unsigned *remove; if (!c) return NULL; remove = malloc(sizeof(unsigned) * asteroids_size); if (!remove) return NULL; removedI = 0; for (i = 0; i < asteroids_size; i++) remove[i] = UINT_MAX; for (i = 0; i < asteroids_size; i++) { for (j = 0; j < asteroids[i].points_size; j++) { aNext = j + 1; if (aNext == asteroids[i].points_size) aNext = 0; minX = fminf(asteroids[i].points[j].x, asteroids[i].points[aNext].x); maxX = fmaxf(asteroids[i].points[j].x, asteroids[i].points[aNext].x); minY = fminf(asteroids[i].points[j].y, asteroids[i].points[aNext].y); maxY = fmaxf(asteroids[i].points[j].y, asteroids[i].points[aNext].y); for (k = 0; k < MAX_LASERS_AT_ONCE; k++) { if (!c->lasers[k].valid) continue; if ((isGreater(c->lasers[k].pos->points[0].x, minX) && isLess(c->lasers[k].pos->points[0].x, maxX) && isGreater(c->lasers[k].pos->points[0].y, minY) && isLess(c->lasers[k].pos->points[0].y, maxY)) || (isGreater(c->lasers[k].pos->points[1].x, minX) && isLess(c->lasers[k].pos->points[1].x, maxX) && isGreater(c->lasers[k].pos->points[1].y, minY) && isLess(c->lasers[k].pos->points[1].y, maxY))) { c->lasers[k].valid = false; c->totalLasers--; remove[removedI++] = asteroids[i].id; } } } } return remove; }
vector<int> BPTree::findToBehindIF(Value key) { vector<int> result; int j = -1; Node *node = NULL; while (true) { do { j++; node = new Node(dbName,bfm->getIndexBlocks(indexName)[j],indexName,tableInstance,n); }while(node->getNodeType() == _NONLEAFNODE); vector<Value> info = node->getInfo(); for (int i = 0; i < info.size() - 1; i+=2) { if (isLess(info[i+1],key)) result.push_back(info[i].getIntKey()); } if (node->getLastPtr().getIntKey() != -1) delete node; else break; } return result; }
list insord2(element e, list l) { list l1 = NULL, root = l; list t; if(empty(l) || !isLess(head(l), e)) //se vuoto o head>e inserisci in testa con cons { t = cons(e, l); return t; } t = cons(e, NULL); while(!empty(l) && isLess(head(l), e))//finchè è pieno e finchè head>e faccio il ciclo { l1=l; //l1 diventa l l____l1======| l = tail(l);//tolgo la testa } l1->next = t; //quando esco avro che l è finita oppure ho trovato head> t->next = l; return root; }
list insord(element e, list l) { list t; if(empty(l) || !isLess(head(l), e)){ t = cons(e, l); //se vuoto o se head<e //t->next = l; return t; } return cons(head(l), insord(e, tail(l)));//ricostruisco con head + insord(tail) }
void BPTree::insert(Value key,PtrType pointer) { PtrType nodePtr = findLeafNode(key); Node node(dbName,nodePtr,indexName,tableInstance,n); if (node.getCount() < (n - 1)) insertLeaf(node,key,pointer); else { //排序 vector<Value> keyList = node.getInfo();//只读键值对 if (isLess(key,keyList[0])) { keyList.insert(keyList.begin(),key); Value temp(_TYPE_INT,pointer); keyList.insert(keyList.begin(),temp); } else { for (int i = (keyList.size() - 1 - 1); i >= 0; i-=2) { if (isLessEqual(keyList.at(i),key)) { Value tempPtr(_TYPE_INT,pointer); keyList.insert(keyList.begin() + i+1,tempPtr); keyList.insert(keyList.begin() + i+2,key); break; } } } //更新尾指针 Node newNode(dbName,indexName,tableInstance,n); PtrType newNodePtr = newNode.getNodePtr(); newNode.setLastPtr(node.getLastPtr()); node.setLastPtr(newNodePtr); //赋值元素到该到的地方 int breakPoint = 0; if (n % 2 == 0) breakPoint = (n /2)*2; else breakPoint = ((n / 2) + 1)*2; vector<Value> temp(keyList.begin(),keyList.begin() + breakPoint); node.set(temp);//只写键值对 vector<Value> temp2(keyList.begin() + breakPoint,keyList.end());//TODO:左闭右开? newNode.set(temp2); insertNonleaf(node,temp2[0],newNodePtr); } }
int JarvisMarch:: index_of_rightmost_point_from(const Vec& q) throw() { int i=0, j; for (j=1; j<size_of_polygon_; j++){ Vec reltoj = relTo(polygon_[j],q); Vec reltoi = relTo(polygon_[i],q); if (isLess(reltoj, reltoi)) { i=j; } } return i; }
bool EyeCalibration::sort(CalibrationPointVector &points, int center_x, int center_y) { int i, j; CalibrationPoint key; int size = points.size( ); for(j = 1; j < size; j++) // Start with 1 (not 0) { key = points[j]; for(i = j - 1; (i >= 0) && (isLess(points[i], key, center_x, center_y)); i--) // Smaller values move up { points[i+1] = points[i]; } points[i+1] = key; //Put key into its proper location } return true; }
PtrType BPTree::findLeafNode(Value key) { Node *node = new Node(dbName,root,indexName,tableInstance,n); parentMap.clear(); ParentMap *tempMap = new ParentMap(); tempMap->nodePtr = root; tempMap->parentPtr = -1; parentMap.push_back(*tempMap); int j = 0; //in nonLeafNode while (node->getNodeType() != _LEAFNODE) { vector<Value> temp = node->getInfo(); int i = 1; for(i = 1; i < temp.size(); i+=2) { if (isLess(key,temp[i]))//只能是小于,因为小于才进前面 break; } if (i >= temp.size()) { delete tempMap; ParentMap *tempMap = new ParentMap(); tempMap->nodePtr = atoi(temp[temp.size() - 1].getKey().c_str()); tempMap->parentPtr = node->getNodePtr(); parentMap.push_back(*tempMap); delete node; Node *node = new Node(dbName,atoi(temp[temp.size() - 1].getKey().c_str()),indexName,tableInstance,n); } else { delete tempMap; ParentMap *tempMap = new ParentMap(); tempMap->nodePtr = atoi(temp[temp.size() - 1].getKey().c_str()); tempMap->parentPtr = node->getNodePtr(); parentMap.push_back(*tempMap); delete node; parentMap[j].nodePtr = atoi(temp[temp.size() - 1].getKey().c_str()); Node *node = new Node(dbName,atoi(temp[i - 1].getKey().c_str()),indexName,tableInstance,n); } } return node->getNodePtr(); }
PtrType BPTree::find(Value key) { Node *node = new Node(dbName,root,indexName,tableInstance,n); //in nonLeafNode while (node->getNodeType() != _LEAFNODE) { vector<Value> temp = node->getInfo(); int i = 1; for(i = 1; i < temp.size(); i+=2) { if (isLess(key,temp[i]))//只能是小于,因为小于才进前面 break; } if (i >= temp.size()) { delete node; Node *node = new Node(dbName,atoi(temp[temp.size() - 1].getKey().c_str()),indexName,tableInstance,n); } else { delete node; Node *node = new Node(dbName,atoi(temp[i - 1].getKey().c_str()),indexName,tableInstance,n); } } //in leafNode vector<Value> temp = node->getInfo(); int i = 1; for(i = 1; i < temp.size(); i+=2) { if (isEqual(key,temp[i])) break; } if (i >= temp.size() ) return -1; else return temp[i-1].getIntKey(); delete node; }
vector<int> BPTree::findToBehind(Value key) { vector<int> result; Node *node = new Node(dbName,findLeafNode(key),indexName,tableInstance,n); while (true) { vector<Value> info = node->getInfo(); for (int i = 0; i < info.size() - 1; i+=2) { if (isLess(key,info[i+1]))//大于 result.push_back(info[i].getIntKey()); } if (node->getLastPtr().getIntKey() != -1) { delete node; Node *node = new Node(dbName,node->getLastPtr().getIntKey(),indexName,tableInstance,n); } else break; } return result; }
void BPTree::insertLeaf(Node node,Value key,PtrType pointer) { vector<Value> keyList = node.getInfo();//只读键值对 if (isLess(key,keyList[0])) { keyList.insert(keyList.begin(),key); Value temp(_TYPE_INT,pointer); keyList.insert(keyList.begin(),temp); } else { for (int i = (keyList.size() - 1 - 1); i >= 0; i-=2) { if (isLessEqual(keyList.at(i),key)) { Value tempPtr(_TYPE_INT,pointer); keyList.insert(keyList.begin() + i+1,tempPtr); keyList.insert(keyList.begin() + i+2,key); break; } } } node.set(keyList); }
void BPTree::insertNonleaf(Node node,Value key,PtrType pointer) { if (node.getNodePtr() == root) { Node newNode(dbName,indexName,tableInstance,n); vector<Value> keyList; Value temp1(_TYPE_INT,node.getNodePtr()); Value temp2(_TYPE_INT,pointer); keyList.push_back(temp1); keyList.push_back(key); keyList.push_back(temp2); newNode.set(keyList); root = newNode.getNodePtr(); //把根节点的更改写入磁盘 int firstBlock = bfm->getIndexBlocks(indexName)[0];//获取开始的块 Node rootNode(dbName,firstBlock,indexName,tableInstance,n); Value temp3(_TYPE_INT,root); vector<Value> temp4; temp4.push_back(temp3); rootNode.set(temp4); } else { Node parentNode(dbName,findParentNode(node.getNodePtr()),indexName,tableInstance,n); if (parentNode.getCount() < n) { vector<Value> keyList = parentNode.getInfo();//只读键值对 if (isLess(key,keyList[0])) { Value tempPtr(_TYPE_INT,pointer); keyList.insert(keyList.begin()+1,tempPtr); keyList.insert(keyList.begin()+1,key); } else { for (int i = (keyList.size() - 1 - 1); i >= 0; i-=2) { if (isLessEqual(keyList.at(i),key)) { Value tempPtr(_TYPE_INT,pointer); keyList.insert(keyList.begin() + i+2,tempPtr);//TODO:最后一个会怎么样? keyList.insert(keyList.begin() + i+2,key); break; } } } parentNode.set(keyList); } else//分裂ParentNode { //排序 vector<Value> keyList = parentNode.getInfo();//只读键值对 if (isLess(key,keyList[0])) { Value tempPtr(_TYPE_INT,pointer); keyList.insert(keyList.begin()+1,tempPtr); keyList.insert(keyList.begin()+1,key); } else { for (int i = (keyList.size() - 1 - 1); i >= 0; i-=2) { if (isLessEqual(keyList.at(i),key)) { Value tempPtr(_TYPE_INT,pointer); keyList.insert(keyList.begin() + i+2,tempPtr);//TODO:最后一个会怎么样? keyList.insert(keyList.begin() + i+2,key); break; } } } //赋值元素到该到的地方 int breakPoint = 0; if (n % 2 == 0) breakPoint = (n /2); else breakPoint = ((n / 2) + 1); vector<Value> temp; temp.push_back(keyList[0]); int j = 1; for (int i = 1; i < breakPoint; i++) { temp.push_back(keyList[j]); temp.push_back(keyList[++j]); } parentNode.set(temp); Value newK = keyList[++j]; vector<Value> temp2; for (int i = j + 1; i < keyList.size(); i++) { temp2.push_back(keyList[i]); } Node newNode(dbName,indexName,tableInstance,n); newNode.set(temp2); insertNonleaf(parentNode,newK,newNode.getNodePtr()); } } }
string Edge::toString() { if(isLess(_ppv,_ppw)) return (**_ppv).getHashKey()+":"+(**_ppw).getHashKey()+"|"+to_string(_weight); else return (**_ppw).getHashKey()+":"+(**_ppv).getHashKey()+"|"+to_string(_weight); }
bool isInvisible(POINT p, POINT display) { return isGreater(p.x, display.x) || isGreater(p.y, display.y) || isLess(p.x, 0.0) || isLess(p.y, 0.0); }
int MeshEdgeElementTable::calcLineIntersections(int elem_index, short elem_dir, Point3& lstart, Point3& ldir, Point3* isec_points) { meshElementCode elem_code = getElementCode(elem_index); if (elem_code < MEC_202 || elem_code >= 303) return 0; const int* nodeIds = getNodeIds(elem_index, elem_dir); Point3& p0 = meshNodes[nodeIds[0]]; Point3& p1 = meshNodes[nodeIds[1]]; Point3& normal = normals[elem_index]; if (elem_dir == -1) scalarmult(-1, normal, normal); // Check end-point cases if ( samepoint(p0, lstart) ){ copy3(p0, *isec_points); return 1; } if ( samepoint(p1, lstart) ){ copy3(p1, *isec_points); return 1; } Point3 edge_dir, l_delta0, tmp; // Edge direction vector (normalized) edge_dir[0] = normal[1]; edge_dir[1] = -1 * normal[0]; edge_dir[2] = 0.0; // Edge length diff3(p1, p0, tmp); double edge_len = dot3(edge_dir, tmp); // Vector l_delta0 = lstart - p0 diff3(lstart, p0, l_delta0); // Check that intersection is "within" the edge // project the intersection point to the edge double t = dot3(edge_dir, l_delta0); if ( isLess(t, 0.0) || isGreater(t, edge_len) ) return 0; // Check that intersection distance from the edge is ok // project intersection point to the edge normal double d = dot3(normal, l_delta0); if (d < 0) d *= -1; if ( isGreater(d, MeshEdgeElementTable::pickingTolerance) ) return 0; // Intersection point is: p0 + t * (p1 - p0) scalarmult(t, edge_dir, tmp); add3(p0, tmp, *isec_points); return 1; }
// NOTE: This should work for all triangle elments (303 - 306), but only // if mid-edge and middle nodes are after "corner" nodes in the node list! // Return nof intersections // int MeshFaceElementTable::calcTriangleLineIntersections(int elem_index, short direction, Point3& lstart, Point3& ldir, Point3* isec_points) { // Ccw ordered nodes (if elem_dir is -1, it is from the parent2 // and nodes are cw oriented and must me reordered Point3& p0 = meshNodes[nodeIds[elem_index][0]]; Point3& p1 = meshNodes[nodeIds[elem_index][1]]; Point3& p2 = meshNodes[nodeIds[elem_index][2]]; Point3& normal = normals[elem_index]; static Point3* points[3]; points[0] = (direction >= 0)? &p0 : &p1; points[1] = (direction >= 0)? &p1 : &p0; points[2] = &p2; #if 0 // If element is looking into wrong direction // // NOTE: This makes picking much faster, so wew have to use it (unless some better // method is found), although it means that elements cannot be selected from 'inside', // which would be quite convenient in some cases! // if ( direction == 1 ) { if ( !isLess(dot3(normal, ldir), 0) ) { return 0; } } else if ( direction == -1 ) { if ( !isGreater(dot3(normal, ldir), 0) ) { return 0; } } #endif // Plane equation for the normal and a point in the plane is; // (r) dot (normal) = (r0) dot (normal) = d // So for the form Ax + By + Cz + D = 0, we have // A = normal[0], B = normal[1], C = normal[2], D = -d double D = -1 * dot3(p0, normal); double numer = dot3(normal, lstart) + D; double denom = dot3(normal, ldir); double t; // Intersection if (denom != 0) { t = - numer / denom; // Line is on the plane } else if (numer == 0) { t = 0.0; // Line is parallel,but not in the plane } else { return 0; } //-Calc intersection point from the line equation Point3 tmp; scalarmult(t, ldir, tmp); Point3 isec_point; add3(lstart, tmp, isec_point); // Finally check if intersection point // is inside the element (triangle) if ( pointInsideTriangle(isec_point, points, centers[elem_index], rSquares[elem_index]) ) { copy3(isec_point, *isec_points); return 1; } else { return 0; } }