Exemple #1
0
void Depth_Class::AdjustYield()
{
	YieldFactor = 1.0;
	NearestNeighbor = 0;

	if(AdjustFisYield == false){return;}

	if(FLUX == 0){return;}

	int MaxFisNucID = 92235;
	double FisRateSum = 0, MaxFisRate = 0;

	for(int i = ActinidePos;i <= NucNum;++i)
	{
		int FisNucID = GetNucId(i) ;
		bool HasYield = false;
		for(int j = 0 ; j < YieldData.size(); ++j)
		{
			if(FisNucID == YieldData[j].FisNucID)
			{
				HasYield = true;
				break;
			}
		}
		if(HasYield)
		{
			continue;
		}
		double FisXs = GetLibFisXs(i);
		FisRateSum = FisRateSum + StepNt[STEP-1][i]*FisXs;
		if(StepNt[STEP-1][i]*FisXs > MaxFisRate)
		{
			MaxFisRate = StepNt[STEP-1][i]*FisXs;
			MaxFisNucID = FisNucID;
		}
	}

	if(FisRateSum == 0)
	{
		YieldFactor = 1.0;
		NearestNeighbor = 0;
		return;
	}

	FindNeighbor(MaxFisNucID);

	int NeiIndex = GetNucIdIndex(NearestNeighbor);
	if(StepNt[STEP-1][NeiIndex] == 0)
	{
		YieldFactor = 1.0;
		NearestNeighbor = 0;
		return;
	}
	YieldFactor = 1.0 + FisRateSum/(StepNt[STEP-1][NeiIndex]*GetLibFisXs(NeiIndex));
}
void ESceneAIMapTool::MakeLinks(u8 side_flag, EMode mode, bool bIgnoreConstraints)
{
	if (!side_flag) return;
	for (AINodeIt it=m_Nodes.begin(); it!=m_Nodes.end(); it++){
    	SAINode* T 						= *it;
    	if ((*it)->flags.is(SAINode::flSelected)){
        	for (int k=0; k<4; k++){
            	if (!(side_flag&fl[k])) continue;
            	switch (mode){
                case mdAppend:{ 
                    SAINode* S 				= FindNeighbor(T,k,bIgnoreConstraints);
                    if (S&&S->flags.is(SAINode::flSelected)) T->n[k] = S;
                }break;
                case mdRemove:{ 
                    SAINode* S 				= FindNeighbor(T,k,bIgnoreConstraints);
                    if (S&&S->flags.is(SAINode::flSelected)) T->n[k] = 0;
                }break;
                case mdInvert:{ 
                    SAINode* S 				= FindNeighbor(T,k,bIgnoreConstraints);
                    if (S){
                    	if (!T->flags.is(fl[k])){ 
                            if (T->n[k]&&S->n[opposite[k]]) continue;
                            SAINode* a			= T->n[k];
                            T->n[k] 			= S->n[opposite[k]];
                            S->n[opposite[k]]	= a;
                        }
	                    S->flags.set(fl[opposite[k]],TRUE);
                    }
                }break;
                }
            }
        }
    }
    // reset processing flag
	for (AINodeIt a_it=m_Nodes.begin(); a_it!=m_Nodes.end(); a_it++)
		(*a_it)->flags.set(SAINode::flN1|SAINode::flN2|SAINode::flN3|SAINode::flN4,FALSE);
    UpdateHLSelected	();
}
Exemple #3
0
		int OcTree::Create(Engine* engine, const TRI* tris, int triCount, int trisPerNode)
		{
			this->engine = engine;

			// Build our root node
			if (!BuildRootNode(tris, triCount))
				return 0;

			// Build tree recursively
			BuildTree((OctNode*)OctNodeList.GetLast(), trisPerNode, tris, triCount);

			// Get the table and find neighbors
			int octCount;
			OctNode** octTable = (OctNode**)OctNodeList.BuildTable(&octCount);

			for (int i = 0; i < octCount; i++)
			{
				OctNode* node = octTable[i];

				for (int j = 0; j < NUM_NEIGHBORS; j++)
				{
					int found = NULL_NODE;
					float foundSize = (float)(unsigned int)(1 << 31); // Max val

					BoxSide side;
					side.SetFromBox(node->BBox, j);

					FindNeighbor(octTable, octTable[0], &side, j, &found, &foundSize);

					node->NeighborIdx[j] = found;

				} // for (j)
			} // for (i)

		
			free(octTable);

			this->triangles = tris;

			// Get the final tables
			GetTables(&octreeTable, &octreeCount, &triangleIdxTable, &triangleIdxCount);



			return 1;
		}
Exemple #4
0
		void OcTree::FindNeighbor(OctNode** octTable, OctNode* node, BoxSide* side, int idx, int* found, float* foundSize)
		{
			if (node && node->ChildIdx[0] != NULL_NODE)
			{
				// loop all childs
				for (int i = 0; i < NUM_CHILDREN; i++)
				{
					// get the current one
					OctNode* child = octTable[node->ChildIdx[i]];

					// Find the side of the test box facing the opposite direction of the current side
					BoxSide testSide;
					int testIdx = GetOppositeIdx(idx);
					testSide.SetFromBox(child->BBox, testIdx);
					
					//  If it's the same size or greater, test to see if planes are neighbors
					if (side->GetSize() <= testSide.GetSize())
					{
						if (side->Neighbors(testSide))
							if (testSide.GetSize() < *foundSize)
							{
								*found = node->ChildIdx[i];
								*foundSize = testSide.GetSize();
							} // if

					// keep searching if we haven't found a prefect fit
					if (*foundSize != side->GetSize())
						FindNeighbor(octTable, child, side, idx, found, foundSize);


					} // if

				} // for (i)

			} // if
		}
int BuildTris (s_trianglevert_t (*x)[3], s_mesh_t *y, byte **ppdata )
{
	int		i, j, k, m;
	int		startv;
	int		len, bestlen, besttype;
	int		bestverts[MAXSTUDIOTRIANGLES];
	int		besttris[MAXSTUDIOTRIANGLES];
	int		peak[MAXSTUDIOTRIANGLES];
	int		type;
	int		total = 0;
	long 	t;
	int		maxlen;

	triangles = x;
	pmesh = y;


	t = time( NULL );

	for (i=0 ; i<pmesh->numtris ; i++)
	{
		neighbortri[i][0] = neighbortri[i][1] = neighbortri[i][2] = -1;
		used[i] = 0;
		peak[i] = pmesh->numtris;
	}

	// printf("finding neighbors\n");
	for (i=0 ; i<pmesh->numtris; i++)
	{
		for (k = 0; k < 3; k++)
		{
			if (used[i] & (1 << k))
				continue;

			FindNeighbor( i, k );
		}
		// printf("%d", used[i] );
	}
	// printf("\n");

	//
	// build tristrips
	//
	numcommandnodes = 0;
	numcommands = 0;
	memset (used, 0, sizeof(used));

	for (i=0 ; i<pmesh->numtris ;)
	{
		// pick an unused triangle and start the trifan
		if (used[i])
		{
			i++;
			continue;
		}

		maxlen = 9999;
		bestlen = 0;
		m = 0;
		for (k = i; k < pmesh->numtris && bestlen < 127; k++)
		{
			int localpeak = 0;

			if (used[k])
				continue;

			if (peak[k] <= bestlen)
				continue;

			m++;
			for (type = 0 ; type < 2 ; type++)
			{
				for (startv =0 ; startv < 3 ; startv++)
				{
					if (type == 1)
						len = FanLength (k, startv);
					else
						len = StripLength (k, startv);
					if (len > 127)
					{
						// skip these, they are too long to encode
					}
					else if (len > bestlen)
					{
						besttype = type;
						bestlen = len;
						for (j=0 ; j<bestlen ; j++)
						{
							besttris[j] = striptris[j];
							bestverts[j] = stripverts[j];
						}
						// printf("%d %d\n", k, bestlen );
					}
					if (len > localpeak)
						localpeak = len;
				}
			}
			peak[k] = localpeak;
			if (localpeak == maxlen)
				break;
		}
		total += (bestlen - 2);

		// printf("%d (%d) %d\n", bestlen, pmesh->numtris - total, i );

		maxlen = bestlen;

		// mark the tris on the best strip as used
		for (j=0 ; j<bestlen ; j++)
			used[besttris[j]] = 1;

		if (besttype == 1)
			commands[numcommands++] = -bestlen;
		else
			commands[numcommands++] = bestlen;

		for (j=0 ; j<bestlen ; j++)
		{
			s_trianglevert_t *tri;

			tri = &triangles[besttris[j]][bestverts[j]];

			commands[numcommands++] = tri->vertindex;
			commands[numcommands++] = tri->normindex;
			commands[numcommands++] = tri->s;
			commands[numcommands++] = tri->t;
		}
		// printf("%d ", bestlen - 2 );
		numcommandnodes++;

		if (t != time(NULL))
		{
			printf("%2d%%\r", (total * 100) / pmesh->numtris );
			t = time(NULL);
		}
	}

	commands[numcommands++] = 0;		// end of list marker

	*ppdata = (byte *)commands;

	// printf("%d %d %d\n", numcommandnodes, numcommands, pmesh->numtris  );
	return numcommands * sizeof( short );
}
Exemple #6
0
/*
 * This function takes the neighbors that are above the threshold and update the current curve based on dtw distances
 * uses parallel
 */
Shapelet* Shapelet::UpdateShapelet(vector<vector< double> > Data, vector<int> Emotions){
    //first round, directly use DTW alignment to align this segment and all its neighbors
    vector<vector<double> > neighbors =  FindNeighbor(Data,Emotions);
    //an double array to store my neighbors matching points
    double* myAlginment = (double*)malloc(sizeof(double)*mySize);
    int* alignmentCount = (int*) malloc(sizeof(int)*mySize);
    //make sure the length is correct
    if (Data.size() != length)
        length = Data.size();


    vector<double> myts ; //get my ts into a vector
    for (int i = 0; i< mySize; i++) {
         //initialize myAlignment and alignmentCount to itself
        myAlginment[i] = ts[i];
        alignmentCount[i] = 1;

        //this part is to initialize myts
        if (ts[i] < 0 )
            myts.push_back(0.0);
        else{
            if (ts[i] > 1 )
                myts.push_back(1.0);
            else
                myts.push_back(ts[i]);
        }
    }




    // cout<<endl;
    
    
    for (int i=0; i< length; i++) {
        if (distance[i] <= threshold &  neighbors.at(i).size() > 1){
            vector<vector<double> > oneAlignment =  DTW_alginment( myts, neighbors.at(i));
            for (int j =0; j  < mySize; j++) {
                for (int k = 0; k < oneAlignment.at(j).size(); k++) {
                    myAlginment[j] += oneAlignment.at(j).at(k);
                    alignmentCount[j]  += 1;
                }
            }
        }
    }

    //must double check to make sure every place have at least one match, and the match is not wrong
    for (int i = 0; i < mySize; i++){
        cout<<i<<",";
        if (myAlginment[i] < 0)
            fprintf(stderr,"the sum of alignment is smaller than 0\n");
        if (alignmentCount[i] <= 0)
            fprintf(stderr,"the count of alignment is smaller than or equal to 0\n");
    }
    cout<<endl;

    
    //now build new 
    vector<double> myts2 ;
    for (int i=0; i< mySize; i++ ){

        myts2.push_back ( myAlginment[i]/(double)(alignmentCount[i]));
    }
    
    
    
    vector<int> MyEmotions;
    for (int i = 0; i< length; i++) {
        MyEmotions.push_back(emotions[i]);
    }
    
    
    
    Shapelet* newShape = new Shapelet(myts2, Data, MyEmotions, myR,  myNormalize);

    
    newShape->FindThresh(Data,Emotions);
    
    
    
    if (newShape -> getSal() > salience){
        do {
            // cout<<" Recurse again "<<endl;
            Shapelet* evenNewer = newShape->UpdateShapelet(Data,Emotions);
            if (evenNewer==NULL) {
                break;
                return newShape;
            }
            newShape->cleanMymemory();
            delete newShape;
            newShape = evenNewer;
        }while(true);
        return newShape;
    }
    else{
        // cout<<"Finished"<<endl;
        newShape->cleanMymemory();
        delete newShape;
        Shapelet  *ptr = NULL;
        return  ptr;
    }
    
}
Exemple #7
0
/*
 This function determines the threshold of which the shapelet should have ( presence/non-presence threshold in terms of dtw distsance)
 The threshold is set to maximize the emotion salience of that shaplet alone
 Threshold is determined based on current
 also initialize the neighbors
 */
void Shapelet::FindThresh(vector< vector< double> > Data,  vector<int> Emotions){
    
    if (length==0){
        length = Data.size();
        distance = (double *) malloc(sizeof(double)* length);
        emotions = (int *) malloc(sizeof(int)*length);
    }
    FindNeighbor( Data,Emotions);

    // cout<<"Now print distance :";
    // for(int ii = 0; ii < length; ii ++){
    //     cout<< distance[ii]<<",";
    // }
    // cout<<"------"<<endl;



    // cout<<"original salience "<<salience<<endl;

    if(emotions ==NULL  || distance == NULL) {
        cout<<" ERROR: please initialize segment with seg, data, emotion , R and normalization decision"<<endl;
        return;
    }


    if (verbose){
        cout<<"Distance:";
        for (int ii = 0 ; ii< length; ii++){
                cout<<distance[ii]<<",";
        }
        cout<<endl;
    }

    vector<int> UniqueEmo;
    vector<int> CountEmo;
    vector< vector<double> > EmoDistance;
    vector< vector<int> > UtteranceIndex;
    vector< IndexedDouble* > sortedValues;
    
    
    double bestThresh= 1000000000;
    double Bestsalience = 0;
    bool reachend = false;
        
    //Each value in emotion label is corresponding to the neighbors label
    
    bool found = false;
    for (int ii = 0; ii< length; ii++) {
        found = false;
        for (int jj=0; jj< UniqueEmo.size(); jj++){
            if (UniqueEmo.at(jj)== emotions[ii]) {
                CountEmo.at(jj) ++;
                EmoDistance.at(jj).push_back(distance[ii]);
                UtteranceIndex.at(jj).push_back(ii);
                // collectiveDistance.push_back(distances[ii]);
                found = true;
            }
        }
        if (!found) {
            UniqueEmo.push_back(emotions[ii]);
            CountEmo.push_back(1);
            //push back the utterance id
            vector<int> uttInd;
            uttInd.push_back(ii);
            UtteranceIndex.push_back(uttInd);
            //push back the emotion distance
            vector<double> tempDist;
            tempDist.push_back(distance[ii]);
            EmoDistance.push_back(tempDist);
            // collectiveDistance.push_back(distances[ii]);
        }
    }
    
    /*
     * Now we start doing salience sweep
     */
    int numEmo = UniqueEmo.size();
    // cout<<"Started sorting -->"<<endl;
    
    for(int i = 0; i < numEmo; i++) {
        sortedValues.push_back(QuitckSort(EmoDistance.at(i)));
    }
  
    
    // cout<<"start looking for threshold ----->"<<endl;
    //initialize pointrs
    //initialize count(emotion and shapelet)
    int ptrs[numEmo];
    int count_es[numEmo];
    for (int i=0; i < numEmo; i++) {
        ptrs[i] = 0;
        count_es[i] = 0;
    }
    
    //initialize count(shapelet)
    int count_s = 0;
    
    
    //initialize p(emotion)
    double p_e[numEmo];
    double sum_e = 0;
    for (int i=0; i < numEmo; i++){
        sum_e += EmoDistance.at(i).size();
    }
    for (int i=0; i < numEmo; i++){
        p_e[i] = (double)(EmoDistance.at(i).size()) / sum_e ;
         // cout<<" the probability of emotion "<< i+1<< " is " << p_e[i]<<endl;
    }
    
    int cc = 0;
    
    do{
        // cout<<cc++<<" round ... ";
        //look through the smallest distance, see which is smallest among the four sorted arrays
        double minDist = 100000000000;
        for (int i =0; i< numEmo; i++) {
            if (ptrs[i] != EmoDistance.at(i).size()){
                if ((sortedValues.at(i) + ptrs[i])->value <= minDist){
                    minDist = (sortedValues.at(i) + ptrs[i])->value;
                }
            }
        }
        
        for (int i =0; i< numEmo; i++) {
            if (ptrs[i] != EmoDistance.at(i).size()){
                if( (sortedValues.at(i) + ptrs[i])->value == minDist){
                    count_es[i] ++;
                    count_s ++;
                    ptrs[i] ++;
                }
            }
        }
        
        if (verbose){
            for (int i =0; i< numEmo; i++) {
                cout<<ptrs[i]<< "  ";
                cout<< " out of " <<EmoDistance.at(i).size()<<endl;
            }
            cout<<endl;
        }

        //INITIAL ENTROPY
        double curSalience[4] = {0,0,0,0};
        //NOW calculate salience
        for (int i =0; i< numEmo; i++) {
              curSalience[i] += -p_e[i] * log(p_e[i]) - (1-p_e[i]) * log(1-p_e[i]);
        }

        // cout<<"Original entropy = "<<curSalience<<endl;
        double testVal = 0.0;

        for (int i =0; i< numEmo; i++) {
            double P_e_given_s = (double)(count_es[i]) / (double)(count_s);
            double P_e_given_nots = (double)(p_e[i] - (double)count_es[i]/(double)sum_e)/((double)(sum_e-count_s)/(double)(sum_e));
            double P_s = (double) count_s/(double) sum_e;
            double P_nots = 1-P_s;

            // double P_not_e_given_s = (double)(sum_e - count_es[i])/(double)(count_s);
           
            if (P_e_given_s != 0) {
                curSalience[i] += P_s*P_e_given_s*log(P_e_given_s);
                if (verbose)
                    testVal += P_s*P_e_given_s*log(P_e_given_s);
            }
            if (P_e_given_nots!= 0) {
               curSalience[i] += P_nots*P_e_given_nots*log(P_e_given_nots);
               if(verbose)
                    testVal += P_nots*P_e_given_nots*log(P_e_given_nots);
            }
        }
        
        if (verbose)
            cout<<" After splitting at "<< minDist <<" entropy  = "<<-testVal<<endl;

        double maxCurSal = 0.0;
        for (int i = 0; i < numEmo  ; i++) {
            if (curSalience[i] > maxCurSal)
                maxCurSal = curSalience[i];
        }

         if (maxCurSal > Bestsalience){
                Bestsalience = maxCurSal;
                bestThresh = minDist;
                occured = count_s;
                 // cout<<"best thrsh so far "<<bestThresh<<" and  best IG "<< Bestsalience <<endl;
         }

        reachend = true;
        for (int i=0; i < numEmo; i++) {
            if (ptrs[i] < EmoDistance.at(i).size()-1)
                reachend = false;
        }
    }while(!reachend);
    
    
    //Now we have finished the threshold sweep
    threshold  = bestThresh;
    salience = Bestsalience;
    
    
    //free each of the indexedDouble
    for(int i=0; i< sortedValues.size(); i++){
        free(sortedValues.at(i));
    }
}