Esempio n. 1
0
	/**
	* @return True if the bounding box collides or is inside this frustum
	*/
	template<class T> bool Frustum<T>::collideWithAABBox(const AABBox<T> &bbox) const
	{
		for (auto &plane : planes)
		{
			const Vector3<T> &normal = plane.getNormal();

			Point3<T> nVertex(bbox.getMax());
			if(normal.X >= 0.0)
			{
				nVertex.X = bbox.getMin().X;
			}
			if(normal.Y >= 0.0)
			{
				nVertex.Y = bbox.getMin().Y;
			}
			if(normal.Z >= 0.0)
			{
				nVertex.Z = bbox.getMin().Z;
			}

			if (plane.distance(nVertex) > 0.0)
			{
				return false;
			}
		}

		return true;
	}
Esempio n. 2
0
//---------------------------------------------------------------------------
void Ball::paint()const {
int num_segments= nVertex();
 glBegin(GL_LINE_LOOP);
 for(int i=0; i<num_segments; i++)
        glVertex2f(vertex[i].x, vertex[i].y);
 glEnd();
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
bool StructGridCutPlane::isCellIntersectedByPlane(const Plane& plane, const Vec3d& cellMinCoord, const Vec3d& cellMaxCoord)
{
    // See http://zach.in.tu-clausthal.de/teaching/cg_literatur/lighthouse3d_view_frustum_culling/index.html

    // Start by finding the "positive vertex" and the "negative vertex" relative to plane normal
    Vec3d pVertex(cellMinCoord);
    Vec3d nVertex(cellMaxCoord);

    if (plane.A() >= 0)
    {
        pVertex.x() = cellMaxCoord.x();
        nVertex.x() = cellMinCoord.x();
    }

    if (plane.B() >= 0)
    {
        pVertex.y() = cellMaxCoord.y();
        nVertex.y() = cellMinCoord.y();
    }

    if (plane.C() >= 0)
    {
        pVertex.z() = cellMaxCoord.z();
        nVertex.z() = cellMinCoord.z();
    }

    // Chek if both positive and negative vertex are on same side of plane
    if (plane.distanceSquared(pVertex) < 0)
    {
        if (plane.distanceSquared(nVertex) < 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    else
    {
        if (plane.distanceSquared(nVertex) >= 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}
void Tarjan(Graph g) {
  /* because we really want to go [1, nVertex], we do [0, nVertex] */
  int V = nVertex(g) + 1;
  /* the stack can't go over the number of vertexes in the graph */
  stack = (int*) calloc(V, sizeof(int));
  in_stack = (int*) calloc(V, sizeof(int));
  translation = (int*) calloc(V, sizeof(int));
  discovery = (int*) malloc(sizeof(int)*V);
  low = (int*) malloc(sizeof(int)*V);

  int i;
  for(i=1; i<V; i++)
    low[i] = INT_MAX;
  for(i=1; i<V; i++)
    discovery[i] = INT_MAX;

  for(i = 1; i < V; i++)
    if (low[i] == INT_MAX)
      tarjanVisit(g, i);

}
Esempio n. 5
0
nMeshInstance *nObjFileLoader::loadObjMesh(string name, string object, double scale, nMeshInstance *inst)
{
	if(!isupper(name[0]) && !(name.substr(1, 3) == "/:" || name.substr(1, 3) == "\\:"))
		name = nGine::instance()->appPath() + name;

	nTimer timer;
	timer.start();

	ifstream file(name.c_str(), ios::in);
	if(!file)
	{
		nGine::instance()->getLogger()->addDebugLog("\nUnable to open " + name);
		return 0;
	}

	nMeshInstance *dp = inst;
	if(!dp)
		dp = new nMeshInstance;
	bool cull  = false;
	string line;

	unsigned int totalVertexCount = 0;
	unsigned int triangleCount = 0;
	while(getline(file, line))
		if(line.substr(0, 1) == "o")
			cull = (line.substr(2) != object && object.size());
		else if(line.substr(0, 2) == "v ")
			totalVertexCount++;
		else if(line.substr(0, 1) == "f" && !cull)
			triangleCount++;

	file.close();
	file.open(name.c_str(), ios::in);

	unsigned int triangles = 0;

	nIndexedTriangle *indexedTriangles = new nIndexedTriangle[triangleCount];

	vector<vector<unsigned int> > index;
	vector<nVertex> vertices;
	vector<nVec3> normals;
	vector<nVec2> coords;
	vector<nMaterial *> materials;
	string obj;
	nMaterial *mat = 0;
	bool smooth = false;

	unsigned int vertexCount = 0;

	unsigned int li = 0;

	index.resize(totalVertexCount);
	//int faceParseType = 0; // 0 = unknown, 1 = position only, 2 = position + normal, 3 = pos + norm + tex

	while(getline(file, line))
	{
		if(line.substr(0, 2) == "vt")
		{
			nVec2 c;
			sscanf(&line[0], "vt %lf %lf", &c.x, &c.y);
			coords.push_back(c);
		}
		else if(line.substr(0, 2) == "vn")
		{
			nVec3 n;
			sscanf(&line[0], "vn %lf %lf %lf", &n.x, &n.y, &n.z);
			normals.push_back(n);
		}
		else if(line.substr(0, 2) == "v ")
		{
			nVec3 v;
			sscanf(&line[0], "v %lf %lf %lf", &v.x, &v.y, &v.z);
			index[vertexCount].push_back(vertices.size());
			vertices.push_back(nVertex(v * scale));
			vertexCount++;
		}
	}

	//cout<<" ["<<timer.elapsed()<<"]";

	file.close();
	file.open(name.c_str(), ios::in);

	while(getline(file, line))
	{
		li++;
		if(line.substr(0, 1) == "o")
		{
			bool culled = cull;
			obj = line.substr(2);
			cull = (obj != object && object.size());
			if(cull && !culled)
				break;
		}
		else if(line.substr(0, 6) == "mtllib")
		{
			string fileName;
			unsigned int backslash = name.find_last_of('\\');
			unsigned int slash = name.find_last_of('/');
			if(backslash != string::npos && (backslash > slash || slash == string::npos))
				fileName = name.substr(0, name.find_last_of('\\')) + "\\" + line.substr(7);
			else if(slash != string::npos)
				fileName = name.substr(0, name.find_last_of('/')) + "/" + line.substr(7);
			else
				fileName = line.substr(7);
			materials = loadMTL(fileName);
		}
		else if(line.substr(0, 6) == "usemtl")
		{
			mat = materials[0];
			for(double i = 0; i != materials.size(); i++)
				if(materials[i]->getName() == line.substr(7))
				{
					mat = materials[i];
					break;
				}
		}
		else if(line.substr(0, 1) == "s")
		{
			smooth = (line != "s off");
		}
		else if(line.substr(0, 1) == "f" && !cull)
		{
			if(line.find("//") != string::npos)
			{
				unsigned int v[6];
				sscanf(&line[0], "f %d//%d %d//%d %d//%d", &v[0], &v[1], &v[2], &v[3], &v[4], &v[5]);
				bool exists[3] = {false, false, false};
				nIndexedTriangle tr;
				tr.material = mat;
				for(unsigned int j = 0; j != 3; j++)
					for(unsigned int i = 0; i != index[v[2 * j] - 1].size(); i++)
						if(vertices[index[v[2 * j] - 1][i]].normal.isNull() || vertices[index[v[2 * j] - 1][i]].normal == normals[v[2 * j + 1] - 1])
						{
							tr.v[j] = index[v[2 * j] - 1][i];
							vertices[index[v[2 * j] - 1][i]].normal = normals[v[2 * j + 1] - 1];
							exists[j] = true;
						}
				for(unsigned int j = 0; j != 3; j++)
					if(!exists[j])
					{
						index[v[2 * j] - 1].push_back(vertices.size());
						tr.v[j] = vertices.size();
						vertices.push_back(nVertex(vertices[v[2 * j] - 1].position, normals[v[2 * j + 1] - 1]));
					}
				indexedTriangles[triangles] = tr;
				triangles++;
			}
			else if(line.find("/") != string::npos)
			{
				unsigned int v[9];
				sscanf(&line[0], "f %d/%d/%d %d/%d/%d %d/%d/%d", &v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &v[6], &v[7], &v[8]);
				bool exists[3] = {false, false, false};
				nIndexedTriangle tr;
				tr.material = mat;
				for(unsigned int j = 0; j != 3; j++)
					for(unsigned int i = 0; i != index[v[3 * j] - 1].size(); i++)
						#ifdef N_MULTI_TEX_COORD
							if((vertices[index[v[3 * j] - 1][i]].normal.isNull() || vertices[index[v[3 * j] - 1][i]].normal == normals[v[3 * j + 2] - 1])
							&& (isinf(vertices[index[v[3 * j] - 1][i]].tex[0].x) || vertices[index[v[3 * j] - 1][i]].tex[0] == coords[v[3 * j + 1] - 1]))
						#else
							if((vertices[index[v[3 * j] - 1][i]].normal.isNull() || vertices[index[v[3 * j] - 1][i]].normal == normals[v[3 * j + 2] - 1])
							&& (isinf(vertices[index[v[3 * j] - 1][i]].coords.x) || vertices[index[v[3 * j] - 1][i]].coords == coords[v[3 * j + 1] - 1]))
						#endif
						{
							tr.v[j] = index[v[3 * j] - 1][i];
							vertices[index[v[3 * j] - 1][i]].normal = normals[v[3 * j + 2] - 1];
							#ifdef N_MULTI_TEX_COORD
								vertices[index[v[3 * j] - 1][i]].tex[0] = coords[v[3 * j + 1] - 1];
							#else
								vertices[index[v[3 * j] - 1][i]].coords = coords[v[3 * j + 1] - 1];
							#endif
							exists[j] = true;
						}
				for(unsigned int j = 0; j != 3; j++)
					if(!exists[j])
					{
						index[v[3 * j] - 1].push_back(vertices.size());
						tr.v[j] = vertices.size();
						vertices.push_back(nVertex(vertices[v[3 * j] - 1].position, normals[v[3 * j + 2] - 1], coords[v[3 * j + 1] - 1]));
					}
				indexedTriangles[triangles] = tr;
				triangles++;
			}
			else
			{
				unsigned int v1[3];
				sscanf(&line[0], "f %d %d %d", &v1[0], &v1[1], &v1[2]);
				indexedTriangles[triangles] = nIndexedTriangle(v1[0] - 1, v1[1] - 1, v1[2] - 1, mat);
				triangles++;
			}
		}
	}
	for(unsigned int i = 0; i != vertices.size(); i++)
		dp->addVertex(vertices[i]);
	for(unsigned int i = 0; i != triangles; i++)
		dp->addTriangle(indexedTriangles[i]);
	delete indexedTriangles;

	for(unsigned int i = 0; i != index.size(); i++)
		index[i].clear();
	index.clear();
	vertices.clear();
	normals.clear();
	coords.clear();
	materials.clear();


	if(object.size())
		dp->computeUnused();

	cout<<"    ["<<timer.reset() * 1000<<"ms]";
	dp->build(nMeshInstance::Mesh);
	cout<<" ["<<timer.elapsed() * 1000<<"ms]"<<endl;

	return dp;

	/*string line;

	while(getline(file, line))
		if(line.substr(0, 1) == "o")
			cull = (line.substr(2) != object && object.size());
		else if(line.substr(0, 2) == "v ")
			dp->setVertexCount(dp->getVertexCount() + 1);
		else if(line.substr(0, 1) == "f" && !cull)
			dp->setTriangleCount(dp->getTriangleCount() + 1);

	file.close();
	file.open(name.c_str(), ios::in);

	unsigned int triangles = 0;

	dp->triangles = new nIndexedTriangle[dp->getTriangleCount()];

	vector<vector<unsigned int> > index;
	vector<nVertex> vertices;
	vector<nVec3> normals;
	vector<nVec2> coords;
	vector<nMaterial *> materials;
	string obj;
	nMaterial *mat = 0;
	bool smooth = false;

	unsigned int vertexCount = 0;

	unsigned int li = 0;

	index.resize(dp->getVertexCount());
	//int faceParseType = 0; // 0 = unknown, 1 = position only, 2 = position + normal, 3 = pos + norm + tex

	while(getline(file, line))
	{
		if(line.substr(0, 2) == "vt")
		{
			nVec2 c;
			sscanf(&line[0], "vt %lf %lf", &c.x, &c.y);
			coords.push_back(c);
		}
		else if(line.substr(0, 2) == "vn")
		{
			nVec3 n;
			sscanf(&line[0], "vn %lf %lf %lf", &n.x, &n.y, &n.z);
			normals.push_back(n);
		}
		else if(line.substr(0, 2) == "v ")
		{
			nVec3 v;
			sscanf(&line[0], "v %lf %lf %lf", &v.x, &v.y, &v.z);
			index[vertexCount].push_back(vertices.size());
			vertices.push_back(nVertex(v * scale));
			vertexCount++;
		}
	}

	//cout<<" ["<<timer.elapsed()<<"]";

	file.close();
	file.open(name.c_str(), ios::in);

	while(getline(file, line))
	{
		li++;
		if(line.substr(0, 1) == "o")
		{
			bool culled = cull;
			obj = line.substr(2);
			cull = (obj != object && object.size());
			if(cull && !culled)
				break;
		}
		else if(line.substr(0, 6) == "mtllib")
		{
			string fileName;
			unsigned int backslash = name.find_last_of('\\');
			unsigned int slash = name.find_last_of('/');
			if(backslash != string::npos && (backslash > slash || slash == string::npos))
				fileName = name.substr(0, name.find_last_of('\\')) + "\\" + line.substr(7);
			else if(slash != string::npos)
				fileName = name.substr(0, name.find_last_of('/')) + "/" + line.substr(7);
			else
				fileName = line.substr(7);
			materials = loadMTL(fileName);
		}
		else if(line.substr(0, 6) == "usemtl")
		{
			mat = materials[0];
			for(double i = 0; i != materials.size(); i++)
				if(materials[i]->getName() == line.substr(7))
				{
					mat = materials[i];
					break;
				}
		}
		else if(line.substr(0, 1) == "s")
		{
			smooth = (line != "s off");
		}
		else if(line.substr(0, 1) == "f" && !cull)
		{
			if(line.find("//") != string::npos)
			{
				unsigned int v[6];
				sscanf(&line[0], "f %d//%d %d//%d %d//%d", &v[0], &v[1], &v[2], &v[3], &v[4], &v[5]);
				bool exists[3] = {false, false, false};
				nIndexedTriangle tr;
				tr.material = mat;
				for(unsigned int j = 0; j != 3; j++)
					for(unsigned int i = 0; i != index[v[2 * j] - 1].size(); i++)
						if(vertices[index[v[2 * j] - 1][i]].normal.isNull() || vertices[index[v[2 * j] - 1][i]].normal == normals[v[2 * j + 1] - 1])
						{
							tr.v[j] = index[v[2 * j] - 1][i];
							vertices[index[v[2 * j] - 1][i]].normal = normals[v[2 * j + 1] - 1];
							exists[j] = true;
						}
				for(unsigned int j = 0; j != 3; j++)
					if(!exists[j])
					{
						index[v[2 * j] - 1].push_back(vertices.size());
						tr.v[j] = vertices.size();
						vertices.push_back(nVertex(vertices[v[2 * j] - 1].position, normals[v[2 * j + 1] - 1]));
					}
				dp->triangles[triangles] = tr;
				triangles++;
			}
			else if(line.find("/") != string::npos)
			{
				unsigned int v[9];
				sscanf(&line[0], "f %d/%d/%d %d/%d/%d %d/%d/%d", &v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &v[6], &v[7], &v[8]);
				bool exists[3] = {false, false, false};
				nIndexedTriangle tr;
				tr.material = mat;
				for(unsigned int j = 0; j != 3; j++)
					for(unsigned int i = 0; i != index[v[3 * j] - 1].size(); i++)
						#ifdef N_MULTI_TEX_COORD
							if((vertices[index[v[3 * j] - 1][i]].normal.isNull() || vertices[index[v[3 * j] - 1][i]].normal == normals[v[3 * j + 2] - 1])
							&& (isinf(vertices[index[v[3 * j] - 1][i]].tex[0].x) || vertices[index[v[3 * j] - 1][i]].tex[0] == coords[v[3 * j + 1] - 1]))
						#else
							if((vertices[index[v[3 * j] - 1][i]].normal.isNull() || vertices[index[v[3 * j] - 1][i]].normal == normals[v[3 * j + 2] - 1])
							&& (isinf(vertices[index[v[3 * j] - 1][i]].coords.x) || vertices[index[v[3 * j] - 1][i]].coords == coords[v[3 * j + 1] - 1]))
						#endif
						{
							tr.v[j] = index[v[3 * j] - 1][i];
							vertices[index[v[3 * j] - 1][i]].normal = normals[v[3 * j + 2] - 1];
							#ifdef N_MULTI_TEX_COORD
								vertices[index[v[3 * j] - 1][i]].tex[0] = coords[v[3 * j + 1] - 1];
							#else
								vertices[index[v[3 * j] - 1][i]].coords = coords[v[3 * j + 1] - 1];
							#endif
							exists[j] = true;
						}
				for(unsigned int j = 0; j != 3; j++)
					if(!exists[j])
					{
						index[v[3 * j] - 1].push_back(vertices.size());
						tr.v[j] = vertices.size();
						vertices.push_back(nVertex(vertices[v[3 * j] - 1].position, normals[v[3 * j + 2] - 1], coords[v[3 * j + 1] - 1]));
					}
				dp->triangles[triangles] = tr;
				triangles++;
			}
			else
			{
				unsigned int v1[3];
				sscanf(&line[0], "f %d %d %d", &v1[0], &v1[1], &v1[2]);
				dp->triangles[triangles] = nIndexedTriangle(v1[0] - 1, v1[1] - 1, v1[2] - 1, mat);
				triangles++;
			}
		}
	}
	dp->setVertexCount(vertices.size());
	dp->vertices = new nVertex[dp->getVertexCount()];
	for(unsigned int i = 0; i != dp->getVertexCount(); i++)
		dp->vertices[i] = vertices[i];

	for(unsigned int i = 0; i != index.size(); i++)
		index[i].clear();
	index.clear();
	vertices.clear();
	normals.clear();
	coords.clear();
	materials.clear();


	if(object.size())
		dp->computeUnused();

	cout<<"    ["<<timer.reset() * 1000<<"ms]";
	dp->build(nMeshInstance::BuildMesh);
	cout<<" ["<<timer.elapsed() * 1000<<"ms]"<<endl;

	return dp;*/
}