예제 #1
0
// Finds the intersection of two lines defined with pairs of points.
Vector Vector::getLinesIntersection(const Vector& first_start, 
                                    const Vector& first_end,
                                    const Vector& second_start,
                                    const Vector& second_end)
{
    Vector v1 = first_end - first_start;
    Vector v2 = second_end - second_start;
    double a1 = v1.getY();
    double b1 = -v1.getX();
    double c1 = vectorProduct(first_end, first_start);
    double a2 = v2.getY();
    double b2 = -v2.getX();
    double c2 = vectorProduct(second_end, second_start);

    // Cramer's rule.
    Vector result;
    double det = a1 * b2 - a2 * b1;

    // No intersection or equal lines
    if (DoubleComparison::areEqual(det, 0))
    {
        result.setX(std::numeric_limits<double>::quiet_NaN());
        return result;
    }

    result.setX(c1 * b2 - c2 * b1);
    result.setY(a1 * c2 - c1 * a2);
    result /= det;
    return result;
}
예제 #2
0
bool Convex::hasCollided(Convex* other)
{
	sf::Vector2f collisionPoint, betweenCorners;


	for (int i = 0; i < other->getPointCount(); i++)
	{
		for (int j = 0; j < getPointCount(); j++)
		{
			if (j < getPointCount() - 1)
			{
				sf::Vector2f collisionPoint(other->getPoint(i).x - getPoint(j).x,
					other->getPoint(i).y - getPoint(j).y);
				sf::Vector2f betweenCorners(getPoint(j + 1).x - getPoint(j).x,
					getPoint(j + 1).y - getPoint(j).y);
			}
			else
			{
				sf::Vector2f collisionPoint(other->getPoint(i).x - getPoint(j).x,
					other->getPoint(i).y - getPoint(j).y);
				sf::Vector2f betweenCorners(getPoint(0).x - getPoint(j).x,
					getPoint(0).y - getPoint(j).y);
			}

			if (vectorProduct(betweenCorners.x, betweenCorners.y, collisionPoint.x, collisionPoint.y) > 0)
			{
				return false;
			}
		}
	}

	for (int i = 0; i < this->getPointCount(); i++)
	{
		for (int j = 0; j < other->getPointCount(); j++)
		{
			if (j < other->getPointCount() - 1)
			{
				sf::Vector2f collisionPoint(this->getPoint(i).x - other->getPoint(j).x,
					this->getPoint(i).y - other->getPoint(j).y);
				sf::Vector2f betweenCorners(other->getPoint(j + 1).x - other->getPoint(j).x,
					other->getPoint(j + 1).y - other->getPoint(j).y);
			}
			else
			{
				sf::Vector2f collisionPoint(this->getPoint(i).x - other->getPoint(j).x,
					this->getPoint(i).y - other->getPoint(j).y);
				sf::Vector2f betweenCorners(other->getPoint(0).x -other->getPoint(j).x,
					other->getPoint(0).y - other->getPoint(j).y);
			}

			if (vectorProduct(betweenCorners.x, betweenCorners.y, collisionPoint.x, collisionPoint.y) > 0)
			{
				return false;
			}
		}
	}
	return true;
}
예제 #3
0
void KinematicMotion::addRotation(const double *vec0, const double *vec1, bool normalized) { // not unique, but shortest path
    // rotation is defined in the CURRENT coordinates system
    double e0[3];
    copyVector(vec0, e0);
    double e1[3];
    copyVector(vec1, e1);
    if (!normalized) {
        normalizeVector(e0);
        normalizeVector(e1);
    }
    // algorithm form Non-linear Modeling and Analysis of Solids and Structures (Steen Krenk) P54
    double e2[3];
    e2[0] = e0[0] + e1[0];
    e2[1] = e0[1] + e1[1];
    e2[2] = e0[2] + e1[2];
    double e2_square = vectorProduct(e2, e2);

    double newRotation[9];

    newRotation[0] = 1.0 + 2.0 * e1[0] * e0[0] - 2.0 / e2_square * e2[0] * e2[0];
    newRotation[1] = 0.0 + 2.0 * e1[0] * e0[1] - 2.0 / e2_square * e2[0] * e2[1];
    newRotation[2] = 0.0 + 2.0 * e1[0] * e0[2] - 2.0 / e2_square * e2[0] * e2[2];

    newRotation[3] = 0.0 + 2.0 * e1[1] * e0[0] - 2.0 / e2_square * e2[1] * e2[0];
    newRotation[4] = 1.0 + 2.0 * e1[1] * e0[1] - 2.0 / e2_square * e2[1] * e2[1];
    newRotation[5] = 0.0 + 2.0 * e1[1] * e0[2] - 2.0 / e2_square * e2[1] * e2[2];

    newRotation[6] = 0.0 + 2.0 * e1[2] * e0[0] - 2.0 / e2_square * e2[2] * e2[0];
    newRotation[7] = 0.0 + 2.0 * e1[2] * e0[1] - 2.0 / e2_square * e2[2] * e2[1];
    newRotation[8] = 1.0 + 2.0 * e1[2] * e0[2] - 2.0 / e2_square * e2[2] * e2[2];

    //R'*(R*x + t) = R'*R*x +  R'*t
    matrixProduct(newRotation, rotationMatrix);
    matrixVectorProduct(newRotation, translationVector);
}
예제 #4
0
///@brief 判断线段与线段是否相交
///@param[in] aa,bb--线段1两个端点, cc,dd--线段1两个端点
///@pre 
///@return true---在线段上, false---不在线段上
///@author DionysosLai,[email protected] 
///@retval  
///@post 
///@version 1.0 
///@data 2014-7-28 17:35
bool segmentLineIsIntersect( const cocos2d::CCPoint& aa, const cocos2d::CCPoint& bb, const cocos2d::CCPoint& cc, const cocos2d::CCPoint& dd )
{
	/// 以两条线段形成的矩形不重合,说明两条线段必然不相交
	if (!isRectsInterSect(aa, bb, cc, dd))
	{
		return false;
	}

	/// 必须二者互相跨立 注意"="的情况。
	if (0 < vectorProduct(aa.x-cc.x, aa.y-cc.y, dd.x-cc.x, dd.y-cc.y) * 
		vectorProduct(dd.x-cc.x, dd.y-cc.y, bb.x-cc.x, bb.y-cc.y) &&
		0 < vectorProduct(cc.x-aa.x, cc.y-aa.y, bb.x-aa.x, bb.y-aa.y) * 
		vectorProduct(bb.x-aa.x, bb.y-aa.y, dd.x-aa.x, dd.y-aa.y))
	{
		return true;
	}
	return false; 
}
예제 #5
0
int intersectFace(point3 *orig, point3 *dir, point3 *v1, point3 *v2, point3 *v3){
	//~ float t;
	float u, v;
	point3 e1, e2, tV, pV, qV;
	float det, invDet;
	
	// find vectors for two edges sharing v2
	e1 = directedVector(v1,v2);
	e2 = directedVector(v1,v3);
	
	// begin calculating determinant - also used to calculate U parameter
	pV = vectorProduct(dir,&e2);
	
	//if determinant is near zero, ray lies in plane of triangle
	det = internalProduct(&e1,&pV);
	
	if(det > -EPSILON && det < EPSILON)
		return 0;
		
	invDet = 1.0 / det;
	//calculate distance from v1 to ray origin
	tV = directedVector(v1, orig);
	
	// calculate U parameter and test bounds
	u = internalProduct(&tV, &pV) * invDet;
	
	if(u < 0.0 || u > 1.0)
		return 0;
		
	// prepare to test V parameter
	qV = vectorProduct(&tV,&e1);
		
	// calculate V parameter and test bounds
	v = internalProduct(dir,&qV) * invDet;
	if(v < 0.0 || u + v > 1.0)
		return 0;
	
	// calculate t, ray intersects triangle 
	//~ t = internalProduct(&e2, &qV) * invDet;
	
	return 1;
}
예제 #6
0
bool straightLineIsIntersect( const cocos2d::CCPoint& p0, const cocos2d::CCPoint& p1, const cocos2d::CCPoint& q0, const cocos2d::CCPoint& q1 )
{
	/// 先判断q0与q1能否组成一条直线
	if (!q0.equals(q1))
	{
		/// 当q0 == p0 q1 == p1时,结果为0。 只需要判断线段是否跨立直线即可。
		if (0 <= vectorProduct(p0.x-q0.x, p0.y-q0.y, q1.x-q0.x, q1.y-q0.y) * 
			vectorProduct(q1.x-q0.x, q1.y-q0.y, p1.x-q0.x, p1.y-q0.y))
		{
/*			CCLOG("straight line and segment line is intesect!");*/
			return false;
		}

/*		CCLOG("straight line and segment line isn't intesect!");*/
		return false;
	}

/*	CCLOG("straigth line cannnot be make up of q0 and q1!");*/
	return false;
}
예제 #7
0
bool Geometry::straightLineIsIntersect( const cocos2d::CCPoint& p0, const cocos2d::CCPoint& p1, const cocos2d::CCPoint& q0, const cocos2d::CCPoint& q1 )
{
	/// 先判断q0与q1能否组成一条直线
	if (!q0.equals(q1))
	{
		/// 当q0 == p0 q1 == p1时,结果为0。 只需要判断线段是否跨立直线即可。
		if (0 <= vectorProduct(p0.x-q0.x, p0.y-q0.y, q1.x-q0.x, q1.y-q0.y) * 
			vectorProduct(q1.x-q0.x, q1.y-q0.y, p1.x-q0.x, p1.y-q0.y))
		{
			/* CCLOG("相交");*/
			return true;
		}

		/* CCLOG("不相交");*/
		return false;
	}

	/*	CCLOG("点q0点q1不能构成一条直线");*/
	return false;
}
예제 #8
0
Graphics::Graphics(QObject *parent) :
    QObject(parent)
{
    /*seting start eye and projection plane positions*/
    eye = QVector3D(0, -1, 0);
    eyePos = -START_RADIUS * eye;    
    distance = 300;
    basePoint = -distance * eye;
    up = QVector3D(0, 0, 1);
    right = vectorProduct(up, eye);
}
예제 #9
0
void Graphics::rotate(QPoint delta)
{
    rotationMatrix = QMatrix4x4();
    rotationMatrix.rotate(delta.x(), up);
    rotationMatrix.rotate(delta.y(), right);
    eye = rotationMatrix.map(eye);
    eyePos = rotationMatrix.map(eyePos);
    basePoint = -distance * eye;
    up = rotationMatrix.map(up);
    right = vectorProduct(up, eye);
}
예제 #10
0
파일: Camera.cpp 프로젝트: ndommanget/demo
// Builds the camera axis from position c, aim (focus) of the camera, and up vector
void Camera::lookAt(const float * const c, const float * const aim, const float * const up)
{
    for (unsigned int iCoord=0 ; iCoord<3; ++iCoord)
    {
        this->c[iCoord]=c[iCoord]; // c : camera position
        this->y[iCoord]=up[iCoord]; // y : up vector
        this->z[iCoord]=c[iCoord]-aim[iCoord]; // z : from aim to camera position
    }
    // Opposite direction of axis z
    float minusZ[]= {-this->z[0], -this->z[1], -this->z[2]};
    // axis x  : from axis y and axis z
    vectorProduct(this->y, this->z, this->x);
    // axis y  : from new axis x and opposite of axis z
    vectorProduct(this->x, minusZ, this->y);
    // Normalizes all axis vectors
    normalize(this->x);
    normalize(this->y);
    normalize(this->z);

    // Builds the new view matrix
    updateView();
}
예제 #11
0
/*
	Applies the changes caused by the collision to both collided convexes
*/
void Convex::applyChanges(sf::Vector2f* collisionNormal, float impulse, sf::Vector2f* collisionPoint, Convex* shapeA, Convex* shapeB)
{
	shapeA->setMassCenter();
	shapeB->setMassCenter();

	sf::Vector2f radiusA(collisionPoint->x - shapeA->_massCenter.x,
						 collisionPoint->y - shapeA->_massCenter.y);
	sf::Vector2f radiusB(collisionPoint->x - shapeB->_massCenter.x,
						 collisionPoint->y - shapeB->_massCenter.y);
	
	float radiusAlength = sqrt(pow(radiusA.x, 2) + pow(radiusA.y, 2));
	float radiusBlength = sqrt(pow(radiusB.x, 2) + pow(radiusB.y, 2));


	//float Ja = _mass * pow(radiusAlength, 2);
	//float Jb = shapeB->_mass * pow(radiusBlength, 2);

	float Ja = 44;
	float Jb = 45;

	shapeA->_velocity.x += impulse / shapeA->_mass * collisionNormal->x;
	shapeA->_velocity.y += impulse / shapeA->_mass * collisionNormal->y;

	shapeB->_velocity.x -= impulse / shapeB->_mass * collisionNormal->x;
	shapeB->_velocity.y -= impulse / shapeB->_mass * collisionNormal->y;



	shapeA->_angularVelocity += impulse / Ja * vectorProduct(radiusA.x,
															 radiusA.y,
															 collisionNormal->x,
															 collisionNormal->y);

	shapeB->_angularVelocity -= impulse / Ja * vectorProduct(radiusB.x,
															  radiusB.y,
															  collisionNormal->x,
															  collisionNormal->y);
}
예제 #12
0
파일: Camera.cpp 프로젝트: portalImac/Alice
void Camera::lookAt(GLfloat * c, GLfloat * aim, GLfloat * up)
{
    for (GLuint i=0 ; i<3; i++)
    {
        this->c[i]=c[i]; // centre de la camera
        this->y[i]=up[i]; // vecteur up
        this->z[i]=c[i]-aim[i]; // créer le vecteur z
    }

    GLfloat zOpp[]={-this->z[0], -this->z[1], -this->z[2]};

    vectorProduct(this->y, this->z, this->x);

    vectorProduct(this->x, zOpp, this->y);

    // On normalise
    normalize(this->x);
    normalize(this->y);
    normalize(this->z);

    // Construire la nouvelle matrice view
    updateView();
}
예제 #13
0
Vector3 Vector3::crossProduct(Vector3 v1, Vector3 v2) {
	vector <vector <float> > vectorProduct(2, vector<float>(2, 0.0));
	vectorProduct[0][0] = v1.y;
	vectorProduct[1][0] = v1.z;
	vectorProduct[0][1] = v2.y;
	vectorProduct[1][1] = v2.z;
	float x = Matrix::twoDeterminant(vectorProduct);

	vectorProduct[0][0] = v1.x;
	vectorProduct[0][1] = v2.x;
	float y = -1*Matrix::twoDeterminant(vectorProduct);

	vectorProduct[1][0] = v1.y;
	vectorProduct[1][1] = v2.y;
	float z = Matrix::twoDeterminant(vectorProduct);

	return Vector3(x, y, z);
}
예제 #14
0
///@brief 判断折线的拐向,
///
///以线段P1为参考,;
///@param[in] p1--线段1,p2--线段2
///@pre P1、P1必须有公共端点
///@return 1--左边 0 -- 共线 -1---右边
///@retval 
///@post
int LineDebug::PolyLineDerection(const CCPoint p1, const CCPoint p0, const CCPoint p2)
{
	float vectorProductResult = 0.0f;

	vectorProductResult = (float)vectorProduct(p1.x-p0.x, p1.y-p0.y, p2.x-p0.x, p2.y-p0.y);
	
	if (vectorProductResult < 1e-3 && vectorProductResult > -1e-3)
	{
		CCLOG("p1p0 and p2p0 is at the same dereciton.");
		return 0;
	}
	else if (vectorProductResult >= 1e-3)
	{
		CCLOG("p2p0 is at p1p0's left dereciotn.");
		return -1;
	}
	else if (vectorProductResult <= -1e-3)
	{
		CCLOG("p2p0 is at p1p0's right dereciotn.");
		return 1;
	}
}
예제 #15
0
int Geometry::polyLineDerection( const cocos2d::CCPoint& p1, const cocos2d::CCPoint& p0, const cocos2d::CCPoint& p2 )
{
	float vectorProductResult = 0.0f;
	int derection = 0;

	vectorProductResult = (float)vectorProduct(p1.x-p0.x, p1.y-p0.y, p2.x-p0.x, p2.y-p0.y);

	if (vectorProductResult < 1 && vectorProductResult > -1)
	{
		derection = 0;	///< 共线
	}
	else if (vectorProductResult >= 1)
	{
		derection = -1;	///< p2p0在p1p0的右手边
	}
	else if(vectorProductResult <= -1)
	{
		derection = 1;	///< p2p0在p1p0的左手边
	}
	
	return derection;
}
예제 #16
0
///@brief 判断折线的拐向,
///以线段P1为参考,;
///@param[in] p1--线段1,p2--线段2
///@pre P1、P1必须有公共端点
///@return 1--左边 0 -- 共线 -1---右边
///@retval 
///@post
///@author DionysosLai,[email protected]等
///@version 1.0
///@data 2014-04-10
int polyLineDerection( const cocos2d::CCPoint& p1, const cocos2d::CCPoint& p0, const cocos2d::CCPoint& p2 )
{
	float vectorProductResult = 0.0f;

	vectorProductResult = (float)vectorProduct(p1.x-p0.x, p1.y-p0.y, p2.x-p0.x, p2.y-p0.y);

	if (vectorProductResult < 1 && vectorProductResult > -1)
	{
		/*		CCLOG("p1p0 and p2p0 is at the same dereciton.");*/
		return 0;
	}

	if (vectorProductResult >= 1)
	{
		/*		CCLOG("p2p0 is at p1p0's left dereciotn.");*/
		return -1;
	}

	if (vectorProductResult <= -1)
	{
		/*		CCLOG("p2p0 is at p1p0's right dereciotn.");*/
		return 1;
	}
}
///@brief 判断点是否在多边形(包括点在边上)
예제 #18
0
/*
	Calculates the coordinates of the collision point
*/
void Convex::convexCollision(Convex* other, sf::RenderWindow* window)
{
	sf::Vector2f collisionNormal;
	float collisionX, collisionY;
	float x1, x2, x3, x4, y1, y2, y3, y4;
	

	if (hasCollided(other))
	{
		for (int i = 0; i < getPointCount(); i++)
		{
			//Retrieves the coordinates of the point i
			x1 = this->getPoint(i).x;
			y1 = this->getPoint(i).y;

			//If the index 'i' equals the amount of points
			//the other end of the line should be the starting point
			if (i == getPointCount() - 1)
			{
				x2 = this->getPoint(0).x;
				y2 = this->getPoint(0).y;
			}
			else
			{
				x2 = this->getPoint(i + 1).x;
				y2 = this->getPoint(i + 1).y;
			}


			for (int j = 0; j < other->getPointCount(); j++)
			{
				x3 = other->getPoint(j).x;
				y3 = other->getPoint(j).y;

				//If the index 'j' equals the amount of points
				//the other end of the line should be the starting point
				if (i == getPointCount() - 1)
				{
					x4 = other->getPoint(0).x;
					y4 = other->getPoint(0).y;
				}
				else
				{
					x4 = other->getPoint(i + 1).x;
					y4 = other->getPoint(i + 1).y;
				}

				collisionX = vectorProduct(vectorProduct(x1, y1, x2, y2),
							 vectorProduct(x1, 1, x2, 1),
							 vectorProduct(x3, y3, x4, y4),
							 vectorProduct(x3, 1, x4, 1))
							 /
							 vectorProduct(vectorProduct(x1, 1, x2, 1),
							 vectorProduct(y1, 1, y2, 1),
							 vectorProduct(x3, 1, x4, 1),
							 vectorProduct(y3, 1, y4, 1));

				collisionY = vectorProduct(vectorProduct(x1, y1, x2, y2),
							 vectorProduct(y1, 1, y2, 1),
							 vectorProduct(x3, y3, x4, y4),
							 vectorProduct(y3, 1, y4, 1))
							 /
							 vectorProduct(vectorProduct(x1, 1, x2, 1),
							 vectorProduct(y1, 1, y2, 1),
							 vectorProduct(x3, 1, x4, 1),
							 vectorProduct(y3, 1, y4, 1));
				
				//Checking that the collision point is actually between the corners
				//of the convexes
				if (isBetween(collisionX, x1, x2) && isBetween(collisionX, x3, x4) &&
					isBetween(collisionY, y1, y2) && isBetween(collisionY, y3, y4))
				{
					//If the collision point is between these points use them to calculate the
					//collisionNormal's components
					if (isBetween(collisionX, x1, x2) && isBetween(collisionY, y1, y2))
					{
						float x = x2 - x1;
						float y = y2 - y1;
						
						float length = sqrt(pow(x, 2) + pow(y, 2));
						float unitX = x / length;
						float unitY = y / length;

						collisionNormal.x = -unitY;
						collisionNormal.y = unitX;

						float impulse = getImpulse(&sf::Vector2f(collisionX, collisionY), &collisionNormal, other, this);
						applyChanges(&collisionNormal, impulse, &sf::Vector2f(collisionX, collisionY), other, this);
					}
					else
					{
						float x = x4 - x3;
						float y = y4 - y3;

						float length = sqrt(pow(x, 2) + pow(y, 2));
						float unitX = x / length;
						float unitY = y / length;

						collisionNormal.x = -unitY;
						collisionNormal.y = unitX;

						float impulse = getImpulse(&sf::Vector2f(collisionX, collisionY), &collisionNormal, this, other);
						applyChanges(&collisionNormal, impulse, &sf::Vector2f(collisionX, collisionY), this, other);
					}
					
					sf::CircleShape circle(5);
					circle.setFillColor(sf::Color::Red);
					circle.setPosition(collisionX, collisionY);
					window->draw(circle);
				}
			}
		}
	}
}
예제 #19
0
void Geometry::tubaoCalcute( const std::vector<cocos2d::CCPoint>* pointIn, std::vector<cocos2d::CCPoint>* pointOut )
{
	std::vector<CCPoint> point;
	point.reserve(pointIn->size());
	for (unsigned int i = 0; i < point.capacity(); ++i)
	{
		point.push_back(pointIn->at(i));
	}
	/// 找到y最小点,如果y最小有好几个,选择最左边的点,即x最小---最小放在第一个
	for (unsigned int i = 1; i < point.size(); ++i)
	{
		if (point.at(0).y > point.at(i).y)
		{
			std::swap(point.at(0), point.at(i));
		}
		else if (point.at(0).y == point.at(i).y)
		{
			if (point.at(0).x < point.at(i).x)
			{
				std::swap(point.at(0), point.at(i));
			}
		}

	}
	/// 对点集point进行排序
	std::vector<float> delta;	///< 记录每个点和最低点的夹角
	delta.reserve(point.size()-1);
	CCPoint posBegin = point.at(0);
	for (unsigned int i = 0; i < point.size()-1; ++i)
	{
		CCPoint pos = point.at(i+1);
		delta.push_back(getAngle(posBegin, pos));
	}
	for (unsigned int i = 0; i < delta.size() -1; ++i)
	{
		float deltai = delta.at(i);
		for (unsigned int j = i+1; j < delta.size(); ++j)
		{
			float deltaj = delta.at(j);
			if (deltai > deltaj)
			{
				std::swap(delta.at(i), delta.at(j));
				deltai = deltaj;
				if (delta.size()-1 == j)
				{
					std::swap(point.at(i+1), point.at(j));
				}		
				else
				{
					std::swap(point.at(i+1), point.at(j+1));
				}
			}
		}
	}
	/// 头三个必然是凸包3个点
	pointOut->push_back(point.at(0));
	pointOut->push_back(point.at(1));
	pointOut->push_back(point.at(2));
	for (unsigned int i = 3; i < point.size(); ++i)
	{
		/// 判断拐向
		CCPoint p0 = pointOut->at(pointOut->size()-2);		///< 凸包栈顶下一个元素
		CCPoint p1 = pointOut->at(pointOut->size()-1);		///< 凸包栈顶元素
		CCPoint p2 = point.at(i);						///< pI点
		while ((float)vectorProduct(p1.x-p0.x, p1.y-p0.y, p2.x-p1.x, p2.y-p1.y) <= 0)	///< 不拐向右侧
		{
			pointOut->pop_back();	///< 凸包弹栈
			p0 = pointOut->at(pointOut->size()-2);		///< 凸包栈顶下一个元素
			p1 = pointOut->at(pointOut->size()-1);		///< 凸包栈顶元素
			p2 = point.at(i);						///< pI点
		}
		pointOut->push_back(point.at(i));
	}
}
예제 #20
0
void getFacesNearToCamera(unsigned vertexesSize, point3 cameraOrigin,float inTexcoords[], float inColors[][4], float inVertexes[],
								float outTexCoords[], float outColors[][4], float outVertexes[], unsigned *finalVertexes){
	std::vector<face> faces;
	
	unsigned saved = 0;

	// we divide vertexes size by 3 because each face has 3 vertexes
	for(unsigned i  = 0; i < vertexesSize/3; ++i){

		// we realize the vector product according to hand right rule
		
		point3 vertexCoord1;
		vertexCoord1.x = inVertexes[9*i];
		vertexCoord1.y = inVertexes[9*i + 1];
		vertexCoord1.z = inVertexes[9*i + 2];
		
		point3 vertexCoord2;
		vertexCoord2.x = inVertexes[9*i + 3];
		vertexCoord2.y = inVertexes[9*i + 4];
		vertexCoord2.z = inVertexes[9*i + 5];
		
		point3 vertexCoord3;
		vertexCoord3.x = inVertexes[9*i + 6];
		vertexCoord3.y = inVertexes[9*i + 7];
		vertexCoord3.z = inVertexes[9*i + 8];

		// this vector comes from p2 and goes to p1
		point3 vT1 = directedVector(&vertexCoord2,&vertexCoord1);
		// this vector comes from p2 and goes to p3
		point3 vT2 = directedVector(&vertexCoord2,&vertexCoord3);
		// we realize the vector product according to hand right rule
		point3 normalOutFace = vectorProduct(&vT2,&vT1);
		
		point3 faceCenter;
		faceCenter.x = (vertexCoord1.x + vertexCoord2.x + vertexCoord3.x)/3.0;
		faceCenter.y = (vertexCoord1.y + vertexCoord2.y + vertexCoord3.y)/3.0;
		faceCenter.z = (vertexCoord1.z + vertexCoord2.z + vertexCoord3.z)/3.0;
		// vector from triangle face to camera origin;
		point3 tF2cO = directedVector(&faceCenter,&cameraOrigin);

		// calcule internal product between gC2TC and normalOutFace
		// if internal product greater or equal than zero 
		//   save the coords that are facing to camera
		//~ if (internalProduct(&tF2cO,&normalOutFace) > 0){
		
		if (internalProduct(&tF2cO,&normalOutFace) > EPSILON){

			point3 dir = directedVector(&cameraOrigin,&faceCenter);
			
			if (intersectFace(&cameraOrigin, &dir, &vertexCoord1, &vertexCoord2, &vertexCoord3) == 1) {
			//~ int k = saved;
			face currFace;
			
			currFace.d = getDistance(faceCenter,cameraOrigin);
			//~ outColors[3*k][0] = inColors[3*i][0];
			//~ outColors[3*k][1] = inColors[3*i][1];
			//~ outColors[3*k][2] = inColors[3*i][2];
			//~ outColors[3*k][3] = inColors[3*i][3];
			//~ outColors[3*k + 1][0] = inColors[3*i + 1][0];
			//~ outColors[3*k + 1][1] = inColors[3*i + 1][1];
			//~ outColors[3*k + 1][2] = inColors[3*i + 1][2];
			//~ outColors[3*k + 1][3] = inColors[3*i + 1][3];
			//~ outColors[3*k + 2][0] = inColors[3*i + 2][0];
			//~ outColors[3*k + 2][1] = inColors[3*i + 2][1];
			//~ outColors[3*k + 2][2] = inColors[3*i + 2][2];
			//~ outColors[3*k + 2][3] = inColors[3*i + 2][3];
			
			currFace.f.p1.x = inVertexes[9*i];
			currFace.f.p1.y = inVertexes[9*i + 1];
			currFace.f.p1.z = inVertexes[9*i + 2];
			currFace.f.p2.x = inVertexes[9*i + 3];
			currFace.f.p2.y = inVertexes[9*i + 4];
			currFace.f.p2.z = inVertexes[9*i + 5];
			currFace.f.p3.x = inVertexes[9*i + 6];
			currFace.f.p3.y = inVertexes[9*i + 7];
			currFace.f.p3.z = inVertexes[9*i + 8];
			
			currFace.t.p1.x = inTexcoords[6*i];
			currFace.t.p1.y  = inTexcoords[6*i + 1];
			currFace.t.p1.z  = inTexcoords[6*i + 2];
			currFace.t.p2.x  = inTexcoords[6*i + 3];
			currFace.t.p2.y  = inTexcoords[6*i + 4];
			currFace.t.p2.z  = inTexcoords[6*i + 5];

			faces.push_back(currFace);
			++saved;
			}
		}
	}
	
	std::sort(faces.begin(),faces.end(),nearCamera);
	
	std::vector<face>::iterator itFaces;
	int k = 0;
	for(itFaces = faces.begin(); itFaces < faces.end(); ++itFaces){
		outVertexes[9*k]=(*itFaces).f.p1.x;
		outVertexes[9*k + 1]=(*itFaces).f.p1.y;
		outVertexes[9*k + 2]=(*itFaces).f.p1.z;
		outVertexes[9*k + 3]=(*itFaces).f.p2.x;
		outVertexes[9*k + 4]=(*itFaces).f.p2.y;
		outVertexes[9*k + 5]=(*itFaces).f.p2.z;
		outVertexes[9*k + 6]=(*itFaces).f.p3.x;
		outVertexes[9*k + 7]=(*itFaces).f.p3.y;
		outVertexes[9*k + 8]=(*itFaces).f.p3.z;
		
		outTexCoords[6*k]=(*itFaces).t.p1.x;
		outTexCoords[6*k + 1]=(*itFaces).t.p1.y;
		outTexCoords[6*k + 2]=(*itFaces).t.p1.z;
		outTexCoords[6*k + 3]=(*itFaces).t.p2.x;
		outTexCoords[6*k + 4]=(*itFaces).t.p2.y;
		outTexCoords[6*k + 5]=(*itFaces).t.p2.z;
		
		++k;
	}

	*finalVertexes = 3*saved;
}
예제 #21
0
/**
* Updates this vector to be the vector product of its current
* value and the given vector.
*/
void Vector3::operator %=(const Vector3 &vector)
{
   *this = vectorProduct(vector);
}