Exemple #1
0
void Syntax::findBrackets(const QString & text, int start, int end, BlockData *blockData)
{
	//blockData->brackets.clear();
	
	if( end<0 || end>text.length() ) end=text.length();
	
	if(start>end) return;
	
	for(int i=0; i<highlightingBracketsRules.size(); i++)
	{
		HighlightingBlockRule rule=highlightingBracketsRules[i];
		
		int startIndex=start;
		
		int index = rule.startPattern.indexIn(text, startIndex);
		
		while( index>-1 && index<end )
		{
			BlockData::Bracket bracket;
			bracket.pos=index;
			bracket.type=i;
			bracket.length=rule.startPattern.matchedLength();
			bracket.startBracketOk=true;
			
			startIndex=index+bracket.length;
			
			insertInOrder(blockData, bracket);
			
			//printf("[Syntax::findBrackets] bracket.pos=%d bracket.type=%d bracket.len=%d bracket.start=%d startIndex=%d\n", bracket.pos, bracket.type, bracket.length, (bracket.startBracketOk), startIndex );
			
			index = rule.startPattern.indexIn(text, startIndex);
		}
		
		startIndex=start;
		
		index = rule.endPattern.indexIn(text, startIndex);
		
		
		
		while( index>-1 && index<end )
		{
			BlockData::Bracket bracket;
			bracket.pos=index;
			bracket.type=i;
			bracket.length=rule.endPattern.matchedLength();
			bracket.startBracketOk=false;
			insertInOrder(blockData, bracket);
			startIndex=index+bracket.length;
			index = rule.endPattern.indexIn(text, startIndex);
		}
	}
}
Exemple #2
0
void polygonIntersection(VECTOR *eyePosition, VECTOR *rayFromEyeDirection, OBJECT *object, LIST_NODE_PTR *intersections) {

    int wasIntersectionWithPlaneFound;
    INTERSECTION intersectionWithPlane, polygonIntersection;

    if (dotProduct(rayFromEyeDirection, &object->normal) < EPSILON) {
        VECTOR newN;
        newN.x = -1 * object->normal.x;
        newN.y = -1 * object->normal.y;
        newN.z = -1 * object->normal.z;
        object->dependedNormal = newN;
        object->dependedComponentD = -1 * object->componentD;
    }else{
        object->dependedNormal = object->normal;
        object->dependedComponentD = object->componentD;
    }

    calculatePlaneIntersection(eyePosition, rayFromEyeDirection, object, &intersectionWithPlane, &wasIntersectionWithPlaneFound);

    if (wasIntersectionWithPlaneFound == 1) {
        //Now we have to return an intersection only if the intersection is INSIDE THE POLYGON (Not just the plane)
        if (isThePointInsideThePolygon(object, &intersectionWithPlane.intersection) == 1) {
            polygonIntersection.t = intersectionWithPlane.t;
            polygonIntersection.intersection = intersectionWithPlane.intersection;

            polygonIntersection.object = *object;
            insertInOrder(intersections, polygonIntersection);
        }
    }

}
Exemple #3
0
BSTree::Node* BSTree::insertInOrder(Node* subTreePtr, Node* newNodePtr)
{
	if (subTreePtr == nullptr)
	{
		return newNodePtr; 
	}
	else if (subTreePtr->pAcct->getAccoutNum() > newNodePtr->pAcct->getAccoutNum())
	{
		Node *temp; 
		temp = insertInOrder(subTreePtr->left, newNodePtr); 
		subTreePtr->left = temp; 
	}
	else
	{
		Node *temp;
		temp = insertInOrder(subTreePtr->right, newNodePtr);
		subTreePtr->right = temp;
	}
}
Exemple #4
0
int updateGetSingleChunk(Packet *pkt, int peerID)
{
  recvWindow *rw = &(downloadPool[peerID].rw);
  int dataSize = getPacketSize(pkt) - 16;
  uint8_t *dataPtr = pkt->payload + 16;
  uint32_t seq = (uint32_t)getPacketSeq(pkt);
  downloadPool[peerID].timeoutCount = 0;
  fprintf(stderr,"Got pkt %d expecting %d\n", seq, rw->nextPacketExpected);
  if(seq >= rw->nextPacketExpected) {
    if((seq > rw->nextPacketExpected) && ((seq - rw->nextPacketExpected) <= INIT_THRESH)) { 
      insertInOrder(&(downloadPool[peerID].cache), newFreePacketACK(seq), seq);
      newPacketACK(rw->nextPacketExpected - 1, downloadPool[peerID].ackSendQueue);
    } else if(seq - rw->nextPacketExpected <= INIT_THRESH) {
      newPacketACK(seq, downloadPool[peerID].ackSendQueue);
      rw->nextPacketExpected =
	flushCache(rw->nextPacketExpected, downloadPool[peerID].ackSendQueue, &(downloadPool[peerID].cache));
    }
    rw->lastPacketRead = seq;
    rw->lastPacketRcvd = seq;
    
    int curChunk = downloadPool[peerID].curChunkID;
    long offset = (seq - 1) * PACKET_DATA_SIZE + BT_CHUNK_SIZE * curChunk;
    FILE *of = getChunk.filePtr;
    fprintf(stderr,"In: %d [%ld-%ld]\n", seq, offset, offset + dataSize);
    if(of != NULL) {
      fseek(of, offset, SEEK_SET);
      fwrite(dataPtr, sizeof(uint8_t), dataSize, of);
    }
    
    if(rw->nextPacketExpected > BT_CHUNK_SIZE / PACKET_DATA_SIZE + 1){
      clearQueue(downloadPool[peerID].timeoutQueue);
      fprintf(stderr,"Chunk finished downloading! Next expected %d thresh %d\n", rw->nextPacketExpected, BT_CHUNK_SIZE / PACKET_DATA_SIZE + 1);
      getChunk.list[curChunk].fetchState = 1;
      downloadPool[peerID].state = 0;
      clearQueue(downloadPool[peerID].timeoutQueue);
      initWindows(&(downloadPool[peerID].rw), &(uploadPool[peerID].sw));
      fprintf(stderr,"Chunk %d fetched!\n", curChunk);
      // fprintf(stderr,"%d More GETs in queue\n", downloadPool[peerID].getQueue->size);
      return 1;
    } else {
      return 0;
    }
  } else { 
    fprintf(stderr,"Received an unexpected packet!"
	    "Expecting %d but received %d!",
	    rw->nextPacketExpected, seq);
    newPacketACK(seq, downloadPool[peerID].ackSendQueue);
    return 0;
  }
}
Exemple #5
0
bool BSTree::Insert(Account *actPtr)
{
	Node *newAct = new Node();
	newAct->pAcct = actPtr;

	root = insertInOrder(root, newAct); 
	if (root == nullptr)
	{
		return false; 
	}

	return true; 


}
Exemple #6
0
/*
 * This method returns the intersection with the sphere if it exist, if an intersection is found the reference paramenter *wasIntersectionFound 
 * is returned with the value of 1, if not intersection was found the the parameter *wasIntersectionFound is returned with the value of 0.
 * The parameter *intersection contains the Point were the ray touch the sphere and it also calculates the norm of the sphere.
 */
void sphereIntersection(VECTOR *eyePosition, VECTOR *rayFromEyeDirection, OBJECT *sphere, LIST_NODE_PTR *intersections) {
    INTERSECTION sphereIntersectionOne, sphereIntersectionTwo;
    long double a, b, c, d; //Represent the parts of the equation

    //a = pow(rayFromEyeDirection.x, 2) + pow(rayFromEyeDirection.y, 2) + pow(rayFromEyeDirection.z, 2);
    a = 1; //ONLY FOR SPHERES
    b = 2 * ((rayFromEyeDirection->x * (eyePosition->x - sphere->sphere.position.x)) +
            (rayFromEyeDirection->y * (eyePosition->y - sphere->sphere.position.y)) +
            (rayFromEyeDirection->z * (eyePosition->z - sphere->sphere.position.z)));
    c = ((pow(eyePosition->x - sphere->sphere.position.x, 2)) +
            (pow(eyePosition->y - sphere->sphere.position.y, 2)) +
            (pow(eyePosition->z - sphere->sphere.position.z, 2)) -
            pow(sphere->sphere.radius, 2));

    d = pow(b, 2) - (4 * a * c);

    if (d > EPSILON) { //Case the ray touch the sphere in two points

        long double t1 = calculateQuadraticEquationResults(&a, &b, &c, 1);
        long double t2 = calculateQuadraticEquationResults(&a, &b, &c, 0);

        /*
         
        if (EPSILON < t1 && t1 < t2) {
            sphereIntersection.t = t1;
            wasIntersectionFound = 1;
        } else if (EPSILON < t2 && t2 < t1) {
            sphereIntersection.t = t2;
            wasIntersectionFound = 1;
        }
         * */
        if (t1 > EPSILON) {
            sphereIntersectionOne.t = t1;
            sphereIntersectionOne.object = *sphere;

            //I have to calculate the point where the intersection was found
            sphereIntersectionOne.intersection.x = eyePosition->x + (sphereIntersectionOne.t * rayFromEyeDirection->x);
            sphereIntersectionOne.intersection.y = eyePosition->y + (sphereIntersectionOne.t * rayFromEyeDirection->y);
            sphereIntersectionOne.intersection.z = eyePosition->z + (sphereIntersectionOne.t * rayFromEyeDirection->z);

            //I also calculate the normal of the sphere
            sphereIntersectionOne.object.normal = calculateSphereNorm(sphereIntersectionOne.intersection, &sphere->sphere);
            sphereIntersectionOne.object.dependedNormal = sphereIntersectionOne.object.normal;

            insertInOrder(intersections, sphereIntersectionOne);
        }
        
        if (t2 > EPSILON) {
            sphereIntersectionTwo.t = t2;
            sphereIntersectionTwo.object = *sphere;

            //I have to calculate the point where the intersection was found
            sphereIntersectionTwo.intersection.x = eyePosition->x + (sphereIntersectionTwo.t * rayFromEyeDirection->x);
            sphereIntersectionTwo.intersection.y = eyePosition->y + (sphereIntersectionTwo.t * rayFromEyeDirection->y);
            sphereIntersectionTwo.intersection.z = eyePosition->z + (sphereIntersectionTwo.t * rayFromEyeDirection->z);

            //I also calculate the normal of the sphere
            sphereIntersectionTwo.object.normal = calculateSphereNorm(sphereIntersectionTwo.intersection, &sphere->sphere);
            sphereIntersectionTwo.object.dependedNormal = sphereIntersectionTwo.object.normal;

            insertInOrder(intersections, sphereIntersectionTwo);
        }
    }


}
Exemple #7
0
void cylinderInterseption(VECTOR *eyePosition, VECTOR *rayFromEyeDirection, OBJECT *cylinder, LIST_NODE_PTR *intersections) {
    INTERSECTION intersectionOne, intersectionTwo;

    //precalculos para la ecuacion
    double a_partCommon, b_partCommon;
    double ax, bx, cx, ay, by, cy, az, bz, cz;

    int insertIntersectionOne = 0, insertIntersectionTwo = 0;

    //en el despeje esta parte es comun a los calculos que se haran despues
    a_partCommon = rayFromEyeDirection->x * cylinder->cylinder.vectorQ.x + rayFromEyeDirection->y * cylinder->cylinder.vectorQ.y + rayFromEyeDirection->z * cylinder->cylinder.vectorQ.z;

    b_partCommon = cylinder->cylinder.vectorQ.x * (eyePosition->x - cylinder->cylinder.position.x) +
            cylinder->cylinder.vectorQ.y * (eyePosition->y - cylinder->cylinder.position.y) +
            cylinder->cylinder.vectorQ.z * (eyePosition->z - cylinder->cylinder.position.z);


    //calculos de a               *****************************************************************************************
    ax = -2 * rayFromEyeDirection->x * (a_partCommon * cylinder->cylinder.vectorQ.x) +
            pow(a_partCommon * cylinder->cylinder.vectorQ.x, 2) + pow(rayFromEyeDirection->x, 2);
    ay = -2 * rayFromEyeDirection->y * (a_partCommon * cylinder->cylinder.vectorQ.y) +
            pow(a_partCommon * cylinder->cylinder.vectorQ.y, 2) + pow(rayFromEyeDirection->y, 2);
    az = -2 * rayFromEyeDirection->z * (a_partCommon * cylinder->cylinder.vectorQ.z) +
            pow(a_partCommon * cylinder->cylinder.vectorQ.z, 2) + pow(rayFromEyeDirection->z, 2);

    //calculos de b                 ****************************************************************************************
    bx = 2 * rayFromEyeDirection->x * eyePosition->x - 2 * rayFromEyeDirection->x * cylinder->cylinder.position.x - 2 * rayFromEyeDirection->x * (b_partCommon * cylinder->cylinder.vectorQ.x)
            - 2 * (a_partCommon * cylinder->cylinder.vectorQ.x) * eyePosition->x + 2 * (a_partCommon * cylinder->cylinder.vectorQ.x) * cylinder->cylinder.position.x + 2 * (a_partCommon * cylinder->cylinder.vectorQ.x) * (b_partCommon * cylinder->cylinder.vectorQ.x);

    by = 2 * rayFromEyeDirection->y * eyePosition->y - 2 * rayFromEyeDirection->y * cylinder->cylinder.position.y - 2 * rayFromEyeDirection->y * (b_partCommon * cylinder->cylinder.vectorQ.y)
            - 2 * (a_partCommon * cylinder->cylinder.vectorQ.y) * eyePosition->y + 2 * (a_partCommon * cylinder->cylinder.vectorQ.y) * cylinder->cylinder.position.y + 2 * (a_partCommon * cylinder->cylinder.vectorQ.y) * (b_partCommon * cylinder->cylinder.vectorQ.y);

    bz = 2 * rayFromEyeDirection->z * eyePosition->z - 2 * rayFromEyeDirection->z * cylinder->cylinder.position.z - 2 * rayFromEyeDirection->z * (b_partCommon * cylinder->cylinder.vectorQ.z)
            - 2 * (a_partCommon * cylinder->cylinder.vectorQ.z) * eyePosition->z + 2 * (a_partCommon * cylinder->cylinder.vectorQ.z) * cylinder->cylinder.position.z + 2 * (a_partCommon * cylinder->cylinder.vectorQ.z) * (b_partCommon * cylinder->cylinder.vectorQ.z);

    //calculos de los c             *****************************************************************************************
    cx = pow(eyePosition->x, 2) + pow(cylinder->cylinder.position.x, 2) + pow(b_partCommon * cylinder->cylinder.vectorQ.x, 2)
            - 2 * eyePosition->x * cylinder->cylinder.position.x - 2 * eyePosition->x * (b_partCommon * cylinder->cylinder.vectorQ.x) + 2 * cylinder->cylinder.position.x * (b_partCommon * cylinder->cylinder.vectorQ.x);

    cy = pow(eyePosition->y, 2) + pow(cylinder->cylinder.position.y, 2) + pow(b_partCommon * cylinder->cylinder.vectorQ.y, 2)
            - 2 * eyePosition->y * cylinder->cylinder.position.y - 2 * eyePosition->y * (b_partCommon * cylinder->cylinder.vectorQ.y) + 2 * cylinder->cylinder.position.y * (b_partCommon * cylinder->cylinder.vectorQ.y);

    cz = pow(eyePosition->z, 2) + pow(cylinder->cylinder.position.z, 2) + pow(b_partCommon * cylinder->cylinder.vectorQ.z, 2)
            - 2 * eyePosition->z * cylinder->cylinder.position.z - 2 * eyePosition->z * (b_partCommon * cylinder->cylinder.vectorQ.z) + 2 * cylinder->cylinder.position.z * (b_partCommon * cylinder->cylinder.vectorQ.z);


    //VARIABLES FINALES
    double a_final, b_final, c_final, discriminating, t1, t2;
    a_final = ax + ay + az;
    b_final = bx + by + bz;
    c_final = (cx + cy + cz) - pow(cylinder->cylinder.radius, 2);


    //DE ESTA FORMULA FINAL, HAGO EL CALCULO DEL DISCRIMINANTE
    discriminating = pow(b_final, 2) - 4 * a_final * c_final;

    //preguntar si existen soluciones
    if (discriminating > EPSILON) {
        //CALCULO T1 y T2
        t1 = (-1 * b_final + sqrt(discriminating)) / (2 * a_final);
        t2 = (-1 * b_final - sqrt(discriminating)) / (2 * a_final);

        //pregunto si el cilindro esta delante del ojo
        if (t1 > EPSILON) {

            //calculo el lugar exacto de la interseccion
            intersectionOne.intersection.x = eyePosition->x + t1 * rayFromEyeDirection->x;
            intersectionOne.intersection.y = eyePosition->y + t1 * rayFromEyeDirection->y;
            intersectionOne.intersection.z = eyePosition->z + t1 * rayFromEyeDirection->z;
            intersectionOne.t = t1;
            intersectionOne.object = *cylinder;

            if (cylinder->cylinder.distance1 != 0 || cylinder->cylinder.distance2 != 0) {

                double distanceOriginTo_XM_YM_ZM; //variable para el calculo de la distancia dentro de la misma linea

                //calculo de d para ver si esta en medio de h1 y h2
                distanceOriginTo_XM_YM_ZM = t1 * a_partCommon + b_partCommon;

                //si resulta que NO se encuentra en medio, ahora calculo con respecto a t2... ya que puedo estar viendo el interior del cilindro
                if (distanceOriginTo_XM_YM_ZM <= cylinder->cylinder.distance2 && distanceOriginTo_XM_YM_ZM >= cylinder->cylinder.distance1) {
                    insertIntersectionOne = 1;
                }

            } else {
                insertIntersectionOne = 1;
            }


            if (insertIntersectionOne == 1) {
                //calculo la normal
                intersectionOne.object.normal.x = (intersectionOne.intersection.x -
                        (cylinder->cylinder.position.x + cylinder->cylinder.vectorQ.x * (intersectionOne.t * a_partCommon + b_partCommon))) / cylinder->cylinder.radius;
                intersectionOne.object.normal.y = (intersectionOne.intersection.y -
                        (cylinder->cylinder.position.y + cylinder->cylinder.vectorQ.y * (intersectionOne.t * a_partCommon + b_partCommon))) / cylinder->cylinder.radius;
                intersectionOne.object.normal.z = (intersectionOne.intersection.z -
                        (cylinder->cylinder.position.z + cylinder->cylinder.vectorQ.z * (intersectionOne.t * a_partCommon + b_partCommon))) / cylinder->cylinder.radius;

                intersectionOne.object.dependedNormal = intersectionOne.object.normal;

                insertInOrder(intersections, intersectionOne);
            }

        }
        
        if (t2 > EPSILON) {

            //calculo el lugar exacto de la interseccion
            intersectionTwo.intersection.x = eyePosition->x + t2 * rayFromEyeDirection->x;
            intersectionTwo.intersection.y = eyePosition->y + t2 * rayFromEyeDirection->y;
            intersectionTwo.intersection.z = eyePosition->z + t2 * rayFromEyeDirection->z;
            intersectionTwo.t = t2;
            intersectionTwo.object = *cylinder;

            if (cylinder->cylinder.distance1 != 0 || cylinder->cylinder.distance2 != 0) {

                double distanceOriginTo_XM_YM_ZM; //variable para el calculo de la distancia dentro de la misma linea

                //calculo de d para ver si esta en medio de h1 y h2
                distanceOriginTo_XM_YM_ZM = t2 * a_partCommon + b_partCommon;

                //si resulta que NO se encuentra en medio, ahora calculo con respecto a t2... ya que puedo estar viendo el interior del cilindro
                if (distanceOriginTo_XM_YM_ZM <= cylinder->cylinder.distance2 && distanceOriginTo_XM_YM_ZM >= cylinder->cylinder.distance1) {
                    insertIntersectionTwo = 1;
                }

            } else {
                insertIntersectionTwo = 1;
            }


            if (insertIntersectionTwo == 1) {
                //calculo la normal
                intersectionTwo.object.normal.x = (intersectionTwo.intersection.x -
                        (cylinder->cylinder.position.x + cylinder->cylinder.vectorQ.x * (intersectionTwo.t * a_partCommon + b_partCommon))) / cylinder->cylinder.radius;
                intersectionTwo.object.normal.y = (intersectionTwo.intersection.y -
                        (cylinder->cylinder.position.y + cylinder->cylinder.vectorQ.y * (intersectionTwo.t * a_partCommon + b_partCommon))) / cylinder->cylinder.radius;
                intersectionTwo.object.normal.z = (intersectionTwo.intersection.z -
                        (cylinder->cylinder.position.z + cylinder->cylinder.vectorQ.z * (intersectionTwo.t * a_partCommon + b_partCommon))) / cylinder->cylinder.radius;

                intersectionTwo.object.dependedNormal = intersectionTwo.object.normal;

                insertInOrder(intersections, intersectionTwo);
            }

        }
    }// end   if (discriminating > EPSILON_ZERO)



}