Пример #1
0
 TreeNode* constructFromPrePost(vector<int>& pre, vector<int>& post) {
     if(pre.size()==0) return NULL;
     TreeNode *root = new TreeNode(*(pre.begin()));
     if(pre.size()==1){
         return root;
     }
     else if(pre.size()==2){
         (root->left) = new TreeNode(pre[1]);
         return root;
     }
     else if(pre.size()==3){
         if(pre[1]==post[0])
         {
             (root->left) = new TreeNode(pre[1]);
             (root->right) = new TreeNode(pre[2]);
         }
         else{
             (root->left) = new TreeNode(pre[1]);
              (root->left)->left = new TreeNode(pre[2]);
         }
         return root;
     }
     vector<int> t_vecpre;
     vector<int> t_vecpost;
     FindVector(pre,post,pre[1],t_vecpre,t_vecpost);  
     root->left = constructFromPrePost(t_vecpre,t_vecpost);
     cout<<"------------"<<endl;
     Display(pre);
     Display(post);
     t_vecpre.clear();
     t_vecpost.clear();
     if(pre.size()<=1) {
         root->right = NULL;
     }
     else{
         FindVector(pre,post,pre[1],t_vecpre,t_vecpost); 
         root->right = constructFromPrePost(t_vecpre,t_vecpost);
     }
     return root;
 }
Пример #2
0
	uint AddWord(
		const uint sentno,
		const uint phrno,
		const AnchoredPhrasePair &app,
		const std::string pos,
		const int historySize
	) {
		WordAlignment wa = app.second.get().getWordAlignment();
		PhraseData sd = app.second.get().getSourcePhrase().get();
		PhraseData td = app.second.get().getTargetPhrase().get();

		uint wordno = app.first.find_first();

		uint addCount = 0;
		for(uint j = 0; j < sd.size(); ++j) {
			// TODO: we could support other conditins here as well!
			if(posTags[sentno][wordno] == pos) {
				for(WordAlignment::const_iterator
					wit = wa.begin_for_source(j);
					wit != wa.end_for_source(j);
					++wit
				) {
					std::string wordPair = sd[j] + "_" + td[*wit];
					long long b = FindVocabularyPosition(wordPair);
					if(b < 0)
						continue;
					SelectedWordVector word(phrno,*wit,sd[j],td[*wit],wordno,size);
					FindVector(b,word.vec);
					selectedWords[sentno].push_back(word);
					selectedWords[sentno].back().similarity = MaxSimilarityWithHistory(
						sentno,
						selectedWords[sentno].size()-1,
						historySize
					);
					// currentScore += word.similarity;
					addCount++;
					//LOG(logger_, debug, "add word " << td[*wit] << " aligned to " << sd[j]);
				}
			}
			wordno++;
		}
		return addCount;
	};
Пример #3
0
// ------------------------------------------------------------------------------------------------
void TempMesh::FixupFaceOrientation()
{
	const IfcVector3 vavg = Center();

	// create a list of start indices for all faces to allow random access to faces
	std::vector<size_t> faceStartIndices(vertcnt.size());
	for( size_t i = 0, a = 0; a < vertcnt.size(); i += vertcnt[a], ++a )
		faceStartIndices[a] = i;

	// list all faces on a vertex
	std::map<IfcVector3, std::vector<size_t>, CompareVector> facesByVertex;
	for( size_t a = 0; a < vertcnt.size(); ++a )
	{
		for( size_t b = 0; b < vertcnt[a]; ++b )
			facesByVertex[verts[faceStartIndices[a] + b]].push_back(a);
	}
	// determine neighbourhood for all polys
	std::vector<size_t> neighbour(verts.size(), SIZE_MAX);
	std::vector<size_t> tempIntersect(10);
	for( size_t a = 0; a < vertcnt.size(); ++a )
	{
		for( size_t b = 0; b < vertcnt[a]; ++b )
		{
			size_t ib = faceStartIndices[a] + b, nib = faceStartIndices[a] + (b + 1) % vertcnt[a];
			const std::vector<size_t>& facesOnB = facesByVertex[verts[ib]];
			const std::vector<size_t>& facesOnNB = facesByVertex[verts[nib]];
			// there should be exactly one or two faces which appear in both lists. Our face and the other side
			std::vector<size_t>::iterator sectstart = tempIntersect.begin();
			std::vector<size_t>::iterator sectend = std::set_intersection(
				facesOnB.begin(), facesOnB.end(), facesOnNB.begin(), facesOnNB.end(), sectstart);

			if( std::distance(sectstart, sectend) != 2 )
				continue;
			if( *sectstart == a )
				++sectstart;
			neighbour[ib] = *sectstart;
		}
	}

	// now we're getting started. We take the face which is the farthest away from the center. This face is most probably
	// facing outwards. So we reverse this face to point outwards in relation to the center. Then we adapt neighbouring 
	// faces to have the same winding until all faces have been tested.
	std::vector<bool> faceDone(vertcnt.size(), false);
	while( std::count(faceDone.begin(), faceDone.end(), false) != 0 )
	{
		// find the farthest of the remaining faces
		size_t farthestIndex = SIZE_MAX;
		IfcFloat farthestDistance = -1.0;
		for( size_t a = 0; a < vertcnt.size(); ++a )
		{
			if( faceDone[a] )
				continue;
			IfcVector3 faceCenter = std::accumulate(verts.begin() + faceStartIndices[a],
				verts.begin() + faceStartIndices[a] + vertcnt[a], IfcVector3(0.0)) / IfcFloat(vertcnt[a]);
			IfcFloat dst = (faceCenter - vavg).SquareLength();
			if( dst > farthestDistance ) { farthestDistance = dst; farthestIndex = a; }
		}

		// calculate its normal and reverse the poly if its facing towards the mesh center
		IfcVector3 farthestNormal = ComputePolygonNormal(verts.data() + faceStartIndices[farthestIndex], vertcnt[farthestIndex]);
		IfcVector3 farthestCenter = std::accumulate(verts.begin() + faceStartIndices[farthestIndex],
			verts.begin() + faceStartIndices[farthestIndex] + vertcnt[farthestIndex], IfcVector3(0.0))
			/ IfcFloat(vertcnt[farthestIndex]);
		// We accapt a bit of negative orientation without reversing. In case of doubt, prefer the orientation given in 
		// the file.
		if( (farthestNormal * (farthestCenter - vavg).Normalize()) < -0.4 )
		{
			size_t fsi = faceStartIndices[farthestIndex], fvc = vertcnt[farthestIndex];
			std::reverse(verts.begin() + fsi, verts.begin() + fsi + fvc);
			std::reverse(neighbour.begin() + fsi, neighbour.begin() + fsi + fvc);
			// because of the neighbour index belonging to the edge starting with the point at the same index, we need to 
			// cycle the neighbours through to match the edges again.
			// Before: points A - B - C - D with edge neighbour p - q - r - s
			// After: points D - C - B - A, reversed neighbours are s - r - q - p, but the should be
			//                r   q   p   s
			for( size_t a = 0; a < fvc - 1; ++a )
				std::swap(neighbour[fsi + a], neighbour[fsi + a + 1]);
		}
		faceDone[farthestIndex] = true;
		std::vector<size_t> todo;
		todo.push_back(farthestIndex);

		// go over its neighbour faces recursively and adapt their winding order to match the farthest face 
		while( !todo.empty() )
		{
			size_t tdf = todo.back();
			size_t vsi = faceStartIndices[tdf], vc = vertcnt[tdf];
			todo.pop_back();

			// check its neighbours
			for( size_t a = 0; a < vc; ++a )
			{
				// ignore neighbours if we already checked them
				size_t nbi = neighbour[vsi + a];
				if( nbi == SIZE_MAX || faceDone[nbi] )
					continue;

				const IfcVector3& vp = verts[vsi + a];
				size_t nbvsi = faceStartIndices[nbi], nbvc = vertcnt[nbi];
				std::vector<IfcVector3>::iterator it = std::find_if(verts.begin() + nbvsi, verts.begin() + nbvsi + nbvc, FindVector(vp));
				ai_assert(it != verts.begin() + nbvsi + nbvc);
				size_t nb_vidx = std::distance(verts.begin() + nbvsi, it);
				// two faces winded in the same direction should have a crossed edge, where one face has p0->p1 and the other
				// has p1'->p0'. If the next point on the neighbouring face is also the next on the current face, we need 
				// to reverse the neighbour
				nb_vidx = (nb_vidx + 1) % nbvc;
				size_t oursideidx = (a + 1) % vc;
				if( FuzzyVectorCompare(1e-6)(verts[vsi + oursideidx], verts[nbvsi + nb_vidx]) )
				{
					std::reverse(verts.begin() + nbvsi, verts.begin() + nbvsi + nbvc);
					std::reverse(neighbour.begin() + nbvsi, neighbour.begin() + nbvsi + nbvc);
					for( size_t a = 0; a < nbvc - 1; ++a )
						std::swap(neighbour[nbvsi + a], neighbour[nbvsi + a + 1]);
				}

				// either way we're done with the neighbour. Mark it as done and continue checking from there recursively
				faceDone[nbi] = true;
				todo.push_back(nbi);
			}
		}

		// no more faces reachable from this part of the surface, start over with a disjunct part and its farthest face
	}
}