소스 검색

- Optimizing the AABB transformation
- Making util namespace a class

Panagiotis Christopoulos Charitos 14 년 전
부모
커밋
45928c23c0

+ 11 - 1
anki/collision/Aabb.cpp

@@ -20,7 +20,7 @@ Aabb Aabb::getTransformed(const Transform& transform) const
 	// if not then we are fucked
 	else
 	{
-		boost::array<Vec3, 8> points = {{
+		/*boost::array<Vec3, 8> points = {{
 			max,
 			Vec3(min.x(), max.y(), max.z()),
 			Vec3(min.x(), min.y(), max.z()),
@@ -36,6 +36,16 @@ Aabb Aabb::getTransformed(const Transform& transform) const
 			points[i].transform(transform);
 		}
 
+		out.set(points);*/
+
+		// Experimental way:
+
+		// obb is the rotated "this"
+		Obb obb = Obb(transform.getOrigin(), transform.getRotation(),
+			((max - min) / 2.0) * transform.getScale());
+
+		boost::array<Vec3, 8> points;
+		obb.getExtremePoints(points);
 		out.set(points);
 	}
 

+ 3 - 3
anki/collision/Obb.h

@@ -99,6 +99,9 @@ class Obb: public CollisionShape
 		template<typename Container>
 		void set(const Container& container);
 
+		/// Get extreme points in 3D space
+		void getExtremePoints(boost::array<Vec3, 8>& points) const;
+
 	public:
 		/// @name Data
 		/// @{
@@ -108,9 +111,6 @@ class Obb: public CollisionShape
 		/// our case)
 		Vec3 extends;
 		/// @}
-
-		/// Get extreme points in 3D space
-		void getExtremePoints(boost::array<Vec3, 8>& points) const;
 };
 /// @}
 

+ 1 - 1
anki/resource/Image.h

@@ -72,7 +72,7 @@ class Image
 		/// Get image size in bytes
 		size_t getDataSize() const
 		{
-			return util::getVectorSizeInBytes(data);
+			return Util::getVectorSizeInBytes(data);
 		}
 
 		DataCompression getDataCompression() const

+ 6 - 6
anki/resource/Mesh.cpp

@@ -50,19 +50,19 @@ void Mesh::createVbos(const MeshData& meshData)
 {
 	vbos[VBO_VERT_INDECES].create(
 		GL_ELEMENT_ARRAY_BUFFER,
-		util::getVectorSizeInBytes(meshData.getVertIndeces()),
+		Util::getVectorSizeInBytes(meshData.getVertIndeces()),
 		&meshData.getVertIndeces()[0],
 		GL_STATIC_DRAW);
 
 	vbos[VBO_VERT_POSITIONS].create(
 		GL_ARRAY_BUFFER,
-		util::getVectorSizeInBytes(meshData.getVertCoords()),
+		Util::getVectorSizeInBytes(meshData.getVertCoords()),
 		&meshData.getVertCoords()[0],
 		GL_STATIC_DRAW);
 
 	vbos[VBO_VERT_NORMALS].create(
 		GL_ARRAY_BUFFER,
-		util::getVectorSizeInBytes(meshData.getVertNormals()),
+		Util::getVectorSizeInBytes(meshData.getVertNormals()),
 		&meshData.getVertNormals()[0],
 		GL_STATIC_DRAW);
 
@@ -70,7 +70,7 @@ void Mesh::createVbos(const MeshData& meshData)
 	{
 		vbos[VBO_VERT_TANGENTS].create(
 			GL_ARRAY_BUFFER,
-			util::getVectorSizeInBytes(meshData.getVertTangents()),
+			Util::getVectorSizeInBytes(meshData.getVertTangents()),
 			&meshData.getVertTangents()[0],
 			GL_STATIC_DRAW);
 	}
@@ -79,7 +79,7 @@ void Mesh::createVbos(const MeshData& meshData)
 	{
 		vbos[VBO_TEX_COORDS].create(
 			GL_ARRAY_BUFFER,
-			util::getVectorSizeInBytes(meshData.getTexCoords()),
+			Util::getVectorSizeInBytes(meshData.getTexCoords()),
 			&meshData.getTexCoords()[0],
 			GL_STATIC_DRAW);
 	}
@@ -88,7 +88,7 @@ void Mesh::createVbos(const MeshData& meshData)
 	{
 		vbos[VBO_VERT_WEIGHTS].create(
 			GL_ARRAY_BUFFER,
-			util::getVectorSizeInBytes(meshData.getVertWeights()),
+			Util::getVectorSizeInBytes(meshData.getVertWeights()),
 			&meshData.getVertWeights()[0],
 			GL_STATIC_DRAW);
 	}

+ 1 - 1
anki/resource/Script.cpp

@@ -11,7 +11,7 @@ namespace anki {
 //==============================================================================
 void Script::load(const char* filename)
 {
-	source = util::readFile(filename);
+	source = Util::readFile(filename);
 	if(source.length() < 1)
 	{
 		throw ANKI_EXCEPTION("Cannot load script \"" + filename + "\"");

+ 1 - 1
anki/resource/ShaderProgram.cpp

@@ -352,7 +352,7 @@ std::string ShaderProgram::createSrcCodeToCache(const char* sProgFPathName,
 		return newfPathName.string();
 	}
 
-	std::string src_ = util::readFile(sProgFPathName);
+	std::string src_ = Util::readFile(sProgFPathName);
 	std::string src = preAppendedSrcCode + src_;
 
 	std::ofstream f(newfPathName.string().c_str());

+ 1 - 1
anki/resource/ShaderProgramPrePreprocessor.cpp

@@ -52,7 +52,7 @@ void ShaderProgramPrePreprocessor::parseFileForPragmas(
 	}
 
 	// load file in lines
-	std::vector<std::string> lines = util::getFileLines(filename.c_str());
+	std::vector<std::string> lines = Util::getFileLines(filename.c_str());
 	if(lines.size() < 1)
 	{
 		throw ANKI_EXCEPTION("File \"" + filename + "\": Cannot open or empty");

+ 2 - 2
anki/scene/ParticleEmitterNode.cpp

@@ -29,7 +29,7 @@ ParticleEmitterNode::~ParticleEmitterNode()
 float ParticleEmitterNode::getRandom(float initial, float deviation)
 {
 	return (deviation == 0.0) ?  initial :
-		initial + util::randFloat(deviation) * 2.0 - deviation;
+		initial + Util::randFloat(deviation) * 2.0 - deviation;
 }
 
 
@@ -80,7 +80,7 @@ void ParticleEmitterNode::init(const char* filename)
 		particles.push_back(particle);
 
 		float mass = particleMass +
-			util::randFloat(particleMassDeviation) * 2.0 -
+			Util::randFloat(particleMassDeviation) * 2.0 -
 			particleMassDeviation;
 
 		RigidBody::Initializer init;

+ 14 - 18
anki/util/Util.cpp

@@ -7,29 +7,32 @@
 
 
 namespace anki {
-namespace util {
 
 
 //==============================================================================
-// randRange                                                                   =
-//==============================================================================
-int randRange(int min, int max)
+int Util::randRange(int min, int max)
 {
 	return (rand() % (max-min+1)) + min ;
 }
 
-uint randRange(uint min, uint max)
+
+//==============================================================================
+uint Util::randRange(uint min, uint max)
 {
 	return (rand() % (max-min+1)) + min ;
 }
 
-float randRange(float min, float max)
+
+//==============================================================================
+float Util::randRange(float min, float max)
 {
 	float r = (float)rand() / (float)RAND_MAX;
 	return min + r * (max - min);
 }
 
-double randRange(double min, double max)
+
+//==============================================================================
+double Util::randRange(double min, double max)
 {
 	double r = (double)rand() / (double)RAND_MAX;
 	return min + r * (max - min);
@@ -37,14 +40,12 @@ double randRange(double min, double max)
 
 
 //==============================================================================
-// readFile                                                                    =
-//==============================================================================
-std::string readFile(const char* filename)
+std::string Util::readFile(const char* filename)
 {
 	std::ifstream file(filename);
 	if (!file.is_open())
 	{
-		throw ANKI_EXCEPTION(std::string("Cannot open file \"") + filename + "\"");
+		throw ANKI_EXCEPTION("Cannot open file \"" + filename + "\"");
 	}
 
 	return std::string((std::istreambuf_iterator<char>(file)),
@@ -53,9 +54,7 @@ std::string readFile(const char* filename)
 
 
 //==============================================================================
-// getFileLines                                                                =
-//==============================================================================
-std::vector<std::string> getFileLines(const char* filename)
+std::vector<std::string> Util::getFileLines(const char* filename)
 {
 	std::vector<std::string> lines;
 	std::ifstream ifs(filename);
@@ -74,14 +73,11 @@ std::vector<std::string> getFileLines(const char* filename)
 
 
 //==============================================================================
-// randFloat                                                                   =
-//==============================================================================
-float randFloat(float max)
+float Util::randFloat(float max)
 {
 	float r = float(rand()) / float(RAND_MAX);
 	return r * max;
 }
 
 
-} // end namesapce
 } // end namespace

+ 24 - 24
anki/util/Util.h

@@ -9,39 +9,39 @@
 namespace anki {
 
 
-/// The namespace contains a few useful functions
-namespace util {
-
+/// Contains a few useful functions
+class Util
+{
+	public:
+		/// Pick a random number from min to max
+		static int randRange(int min, int max);
 
-/// Pick a random number from min to max
-extern int randRange(int min, int max);
+		/// Pick a random number from min to max
+		static uint randRange(uint min, uint max);
 
-/// Pick a random number from min to max
-extern uint randRange(uint min, uint max);
+		/// Pick a random number from min to max
+		static float randRange(float min, float max);
 
-/// Pick a random number from min to max
-extern float randRange(float min, float max);
+		/// Pick a random number from min to max
+		static double randRange(double min, double max);
 
-/// Pick a random number from min to max
-extern double randRange(double min, double max);
+		static float randFloat(float max);
 
-extern float randFloat(float max);
+		/// Open a text file and return its contents into a string
+		static std::string readFile(const char* filename);
 
-/// Open a text file and return its contents into a string
-extern std::string readFile(const char* filename);
+		/// Open a text file and return its lines into a string vector
+		static std::vector<std::string> getFileLines(const char* filename);
 
-/// Open a text file and return its lines into a string vector
-extern std::vector<std::string> getFileLines(const char* filename);
+		/// Get the size in bytes of a vector
+		template<typename Vec>
+		static size_t getVectorSizeInBytes(const Vec& v)
+		{
+			return v.size() * sizeof(typename Vec::value_type);
+		}
+};
 
-/// Get the size in bytes of a vector
-template<typename Vec>
-size_t getVectorSizeInBytes(const Vec& v)
-{
-	return v.size() * sizeof(typename Vec::value_type);
-}
 
-
-} // end namespace
 } // end namespace
 
 

+ 1 - 1
testapp/Main.cpp

@@ -342,7 +342,7 @@ void mainLoopExtra()
 	if(InputSingleton::get().getKey(SDL_SCANCODE_Y) == 1)
 	{
 		ANKI_INFO("Exec script");
-		ScriptManagerSingleton::get().execScript(util::readFile("test.py").c_str());
+		ScriptManagerSingleton::get().execScript(Util::readFile("test.py").c_str());
 	}
 
 	mover->getLocalTransform().getRotation().reorthogonalize();

+ 1 - 1
unit-tests/Math/MathCommon.ut.h

@@ -13,7 +13,7 @@ inline float randFloat()
 	float f;
 	while(true)
 	{
-		f = util::randRange(RANGE_MIN, RANGE_MAX);
+		f = Util::randRange(RANGE_MIN, RANGE_MAX);
 		if(!isZero(f))
 		{
 			break;