Commits

Lazar Sumar  committed 2e9ed36

Adding first library files and Visual Studio 2010 solution.

  • Participants

Comments (0)

Files changed (9)

File Math/Geometry.cpp

+#include "Geometry.h"
+
+Line::Line() {
+	// construct Vector3's by default
+}
+
+Line::Line(Vector3 point, Vector3 direction) : point(point), direction(direction) {
+
+}
+
+Vector3 Line::getPoint(double alpha) const {
+	return point + direction*alpha;
+}
+
+double Line::intersectionAlpha(const Plane& plane) const {
+	double alpha, den, num;
+
+	den = direction*plane.normal;
+	if (den == 0.0) {
+		throw ParallelException();
+	}
+
+	num = (plane.point - point)*plane.normal;
+
+	alpha = num / den;
+
+	return alpha;
+}
+
+Vector3 Line::intersect(const Plane& plane) const {
+	double alpha = intersectionAlpha(plane);
+	
+	return getPoint(alpha);
+}
+
+double Line::projectionAlpha(const Line& line) const {
+	Plane plane(line.point, direction, line.direction); // plane through "this" line and "this" point
+
+	return intersectionAlpha(plane);
+}
+
+Vector3 Line::project(const Line& line) const {
+	double alpha = projectionAlpha(line);
+	return getPoint(alpha);
+}
+
+// =========================================================
+
+Plane::Plane() {
+	// construct Vector3's by default
+}
+
+Plane::Plane(Vector3 point, Vector3 normal) : point(point), normal(normal) {
+	if (normal.isZero()) {
+		throw ParallelException();
+	}
+}
+
+Plane::Plane(Vector3 point, Vector3 vector1, Vector3 vector2) : point(point) {
+	normal = vector1^vector2; // cross product
+	if (normal.isZero()) {
+		throw ParallelException();
+	}
+}
+
+Vector3 Plane::intersect(const Line& line) const {
+	return line.intersect(*this);
+}

File Math/Geometry.h

+#pragma once
+
+#include <limits>
+#include "Vector3.h"
+
+using namespace std;
+
+#define PARALLEL_EXCEPTION_MSG "No intersection. Objects are parallel."
+class ParallelException : exception {
+public:
+	ParallelException() : exception(PARALLEL_EXCEPTION_MSG) {}
+	ParallelException(const ParallelException& p) : exception(p) {}
+};
+
+class Plane;
+
+class Line {
+public:
+	Vector3 point, direction;
+	Line();
+	Line(Vector3 point, Vector3 direction);
+
+	Vector3 getPoint(double alpha) const;
+
+	double intersectionAlpha(const Plane& plane) const;
+	Vector3 intersect(const Plane& plane) const;
+	
+	double projectionAlpha(const Line& line) const;
+	Vector3 project(const Line& line) const;
+};
+
+class Plane {
+public:
+	Vector3 point, normal;
+
+	Plane();
+	Plane(Vector3 point, Vector3 normal);
+	Plane(Vector3 point, Vector3 vector1, Vector3 vector2);
+
+	Vector3 intersect(const Line& line) const;
+};

File Math/Vector3.cpp

+/** class:     Vector3
+  * Author:    Lazar Sumar
+  * Date:      10th August 2011
+  * Copyright: GPL, use but please give me due credit.
+  */
+#include "Vector3.h"
+
+Vector3::Vector3() {
+	_x = _y = _z = 0.0;
+}
+
+Vector3::Vector3(double x, double y, double z) {
+	_x = x;
+	_y = y;
+	_z = z;
+}
+
+Vector3::Vector3(const Vector3& v) {
+	_x = v._x;
+	_y = v._y;
+	_z = v._z;
+}
+
+Vector3::Vector3(const Vector3* v) {
+	_x = v->_x;
+	_y = v->_y;
+	_z = v->_z;
+}
+
+Vector3::~Vector3() {
+	// Empty
+}
+
+Vector3& Vector3::operator=(const Vector3& v) {
+	_x = v._x;
+	_y = v._y;
+	_z = v._z;
+
+	return *this;
+}
+
+// Getters
+double Vector3::getX() const {
+	return _x;
+}
+
+double Vector3::getY() const {
+	return _y;
+}
+
+double Vector3::getZ() const {
+	return _z;
+}
+
+string Vector3::toString() const {
+	string s = "[";
+	
+	s += double2string(_x);
+	s += ",";
+	s += double2string(_y);
+	s += ",";
+	s += double2string(_z);
+	s += "]";
+
+	return s;
+}
+
+// member functions and operations on vectors
+double Vector3::length() const {
+	return sqrt(_x*_x + _y*_y + _z*_z);
+}
+
+double Vector3::length2() const {
+	return _x*_x + _y*_y + _z*_z;
+}
+
+bool Vector3::isZero() const {
+	return (_x == 0.0 && _y == 0.0 && _z == 0.0);
+}
+
+Vector3 Vector3::unit() const {
+	double l = length();
+	
+	return Vector3(_x / l, _y / l, _z / l);
+}
+
+
+double Vector3::dot(const Vector3& v) const {
+	return _x*v._x + _y*v._y + _z*v._z;
+}
+
+Vector3 Vector3::cross(const Vector3& v) const {
+	Vector3 vec(this);
+
+	vec ^= v;
+
+	return vec;
+}
+
+// Scalar assignment operators
+Vector3 Vector3::operator*=(double s) { // scalar multiplication v*s
+	_x *= s;
+	_y *= s;
+	_z *= s;
+
+	return *this;
+}
+
+// Vector assignment operators
+Vector3 Vector3::operator+=(const Vector3& v) { // vector addition
+	_x += v._x;
+	_y += v._y;
+	_z += v._z;
+
+	return *this;
+}
+
+Vector3 Vector3::operator-=(const Vector3& v) { // vector subtraction
+	_x -= v._x;
+	_y -= v._y;
+	_z -= v._z;
+
+	return *this;
+}
+
+Vector3 Vector3::operator^=(const Vector3& v) { // cross product
+	double x, y, z;
+
+	x = _y*v._z - _z*v._y;
+	y = _x*v._z - _z*v._x;
+	z = _x*v._y - _y*v._x;
+
+	_x = x;
+	_y = y;
+	_z = z;
+
+	return *this;
+}
+
+
+
+// Scalar operators
+Vector3 Vector3::operator*(double s) const {
+	Vector3 v(this);
+
+	v *= s;
+
+	return v;
+}
+
+
+// Vector operators
+Vector3 Vector3::operator+(const Vector3& v) const {
+	Vector3 vec(this);
+
+	vec += v;
+
+	return v;
+}
+
+Vector3 Vector3::operator-(const Vector3& v) const {
+	Vector3 vec(this);
+
+	vec -= v;
+
+	return v;
+}
+
+Vector3 Vector3::operator-() const {
+	return Vector3(-_x, -_y, -_z);
+}
+
+double Vector3::operator*(const Vector3& v) const { // dot product
+	return this->dot(v);
+}
+
+Vector3 Vector3::operator^(const Vector3& v) const  { // cross product
+	return this->cross(v);
+}
+
+// Friend of the Vector3 class
+Vector3 operator*(double s, const Vector3& v) { // commutative counterpart to v*s
+	return v*s;
+}
+
+Vector3& Vector3::operator++() {   // pre increment operator
+	_x++;
+	_y++;
+	_z++;
+
+	return *this;
+}
+
+Vector3 Vector3::operator++(int i) { // post increment operator
+	Vector3 old(*this);
+
+	_x++;
+	_y++;
+	_z++;
+
+	return old;
+}
+
+Vector3& Vector3::operator--() {   // pre decrement operator
+	_x--;
+	_y--;
+	_z--;
+
+	return *this;
+}
+
+Vector3 Vector3::operator--(int i) { // post decrement operator
+	Vector3 old(*this);
+
+	_x--;
+	_y--;
+	_z--;
+
+	return old;
+}
+
+istream& operator>>(istream& is, Vector3& v) {
+	char c;
+	string s;
+	double x, y, z;
+	is.get(c);
+	if (c!='[') {
+		is.fail();
+	}
+	for (s = "", is.get(c); is.good(); is.get(c)) {
+		if (c == ',') break;
+		if ((c < '0' || c > '9') && (c != '.')) {
+			is.fail();
+			return is;
+		}
+		s += c;
+	}
+	x = string2double(s);
+
+	for (s = "", is.get(c); is.good(); is.get(c)) {
+		if (c == ',') break;
+		if ((c < '0' || c > '9') && (c != '.')) {
+			is.fail();
+			return is;
+		}
+		s += c;
+	}
+	y = string2double(s);
+
+	for (s = "", is.get(c); is.good(); is.get(c)) {
+		if (c == ']') break;
+		if ((c < '0' || c > '9') && (c != '.')) {
+			is.fail();
+			return is;
+		}
+		s += c;
+	}
+	z = string2double(s);
+
+	v._x = x;
+	v._y = y;
+	v._z = z;
+
+	return is;
+}
+
+ostream& operator<<(ostream& os, const Vector3& v) {
+	os << "[" << v._x << "," << v._y << "," << v._z << "]";
+
+	return os;
+}
+
+

File Math/Vector3.h

+/** class:     Vector3
+  * Author:    Lazar Sumar
+  * Date:      10th August 2011
+  * Copyright: GPL, use but please give me due credit.
+  */
+#pragma once
+
+#include <iostream>
+#include <math.h>
+#include "../Util/NumericStrings.h"
+
+using namespace std;
+
+class Vector3 {
+private:
+	double _x, _y, _z;
+public:
+	Vector3();
+	Vector3(double x, double y, double z);
+	Vector3(const Vector3& v);
+	Vector3(const Vector3* v);
+	~Vector3();
+
+	Vector3& operator=(const Vector3& v);
+
+	// Getters
+	double getX() const;
+	double getY() const;
+	double getZ() const;
+
+	string toString() const;
+
+	// member functions and operations on vectors
+	double length() const; // Euclidian length
+	double length2() const; // Euclidian length squared
+	bool isZero() const; // Test for null vector (zero vector) (0, 0, 0)
+	Vector3 unit() const; // returns the unit vector
+
+	double dot(const Vector3& v) const;
+	Vector3 cross(const Vector3& v) const;
+
+	// Scalar assignment operators
+	Vector3 operator*=(double s); // scalar multiplication v*s
+	
+	// Vector assignment operators
+	Vector3 operator+=(const Vector3& v); // vector addition
+	Vector3 operator-=(const Vector3& v); // vector subtraction
+	Vector3 operator^=(const Vector3& v); // cross product
+
+	// Scalar operators
+	Vector3 operator*(double s) const; // scalar multiplication v*s
+	friend Vector3 operator*(double s, const Vector3& v); // scalar multiplication s*v
+
+	// Vector operators
+	Vector3 operator+(const Vector3& v) const; // vector addition
+	Vector3 operator-(const Vector3& v) const; // vector subtraction
+	Vector3 operator-() const;  // vector negation (reverse direction)
+	double operator*(const Vector3& v) const; // dot product
+	Vector3 operator^(const Vector3& v) const; // cross product
+
+	// Comparison operators
+	int operator==(const Vector3& v) const;
+	int operator!=(const Vector3& v) const;
+
+	// Other operators
+	Vector3& operator++();   // pre increment operator
+	Vector3 operator++(int); // post increment operator
+	Vector3& operator--();   // pre decrement operator
+	Vector3 operator--(int); // post decrement operator
+
+	// Stream operators
+	friend istream& operator>>(istream& is, Vector3& v);
+	friend ostream& operator<<(ostream& os, const Vector3& v);
+};
+
+istream& operator>>(istream& is, Vector3& v);
+ostream& operator<<(ostream& os, const Vector3& v);

File Util/NumericStrings.cpp

+#include "NumericStrings.h"
+
+double string2double(string s) {
+	double d;
+	stringstream ssout(s);
+	ssout>>d;
+	return d;
+}
+
+string double2string(double d) {
+	stringstream ss;
+	ss<<d;
+	return ss.str();
+}
+
+long string2long(string s) {
+	long l;
+	stringstream ssout(s);
+	ssout>>l;
+	return l;
+}
+
+string long2string(long l) {
+	stringstream ss;
+	ss<<l;
+	return ss.str();
+}

File Util/NumericStrings.h

+#pragma once
+
+#include <sstream>
+#include <string>
+
+using namespace std;
+
+// String stream conversions
+/*template<class T> T stringTo(const string str) {
+	T temp;
+	stringstream ssout(str);
+	ssout>>temp;
+	return temp;
+}
+
+template<class T> string stringFrom(const T input) {
+	stringstream ss;
+	ss<<input;
+	return ss.str();
+}*/
+
+double string2double(string s);
+string double2string(double d);
+long string2long(string s);
+string long2string(long l);

File vs2010/bibliotheca.sln

+
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 2010
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "bibliotheca", "bibliotheca\bibliotheca.vcxproj", "{3C848D20-A45D-46D8-AC7E-2FCA9785DE7C}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Win32 = Debug|Win32
+		Release|Win32 = Release|Win32
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{3C848D20-A45D-46D8-AC7E-2FCA9785DE7C}.Debug|Win32.ActiveCfg = Debug|Win32
+		{3C848D20-A45D-46D8-AC7E-2FCA9785DE7C}.Debug|Win32.Build.0 = Debug|Win32
+		{3C848D20-A45D-46D8-AC7E-2FCA9785DE7C}.Release|Win32.ActiveCfg = Release|Win32
+		{3C848D20-A45D-46D8-AC7E-2FCA9785DE7C}.Release|Win32.Build.0 = Release|Win32
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+EndGlobal

File vs2010/bibliotheca/bibliotheca.vcxproj

+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <ItemGroup Label="ProjectConfigurations">
+    <ProjectConfiguration Include="Debug|Win32">
+      <Configuration>Debug</Configuration>
+      <Platform>Win32</Platform>
+    </ProjectConfiguration>
+    <ProjectConfiguration Include="Release|Win32">
+      <Configuration>Release</Configuration>
+      <Platform>Win32</Platform>
+    </ProjectConfiguration>
+  </ItemGroup>
+  <PropertyGroup Label="Globals">
+    <ProjectGuid>{3C848D20-A45D-46D8-AC7E-2FCA9785DE7C}</ProjectGuid>
+    <RootNamespace>bibliotheca</RootNamespace>
+  </PropertyGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
+    <ConfigurationType>StaticLibrary</ConfigurationType>
+    <UseDebugLibraries>true</UseDebugLibraries>
+    <CharacterSet>MultiByte</CharacterSet>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>false</UseDebugLibraries>
+    <WholeProgramOptimization>true</WholeProgramOptimization>
+    <CharacterSet>MultiByte</CharacterSet>
+  </PropertyGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
+  <ImportGroup Label="ExtensionSettings">
+  </ImportGroup>
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
+  <PropertyGroup Label="UserMacros" />
+  <PropertyGroup />
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <Optimization>Disabled</Optimization>
+    </ClCompile>
+    <Link>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <Optimization>MaxSpeed</Optimization>
+      <FunctionLevelLinking>true</FunctionLevelLinking>
+      <IntrinsicFunctions>true</IntrinsicFunctions>
+    </ClCompile>
+    <Link>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+      <EnableCOMDATFolding>true</EnableCOMDATFolding>
+      <OptimizeReferences>true</OptimizeReferences>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemGroup>
+    <ClInclude Include="..\..\Math\Geometry.h" />
+    <ClInclude Include="..\..\Math\Vector3.h" />
+    <ClInclude Include="..\..\Util\NumericStrings.h" />
+  </ItemGroup>
+  <ItemGroup>
+    <ClCompile Include="..\..\Math\Geometry.cpp" />
+    <ClCompile Include="..\..\Math\Vector3.cpp" />
+    <ClCompile Include="..\..\Util\NumericStrings.cpp" />
+  </ItemGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+  <ImportGroup Label="ExtensionTargets">
+  </ImportGroup>
+</Project>

File vs2010/bibliotheca/bibliotheca.vcxproj.filters

+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <ItemGroup>
+    <Filter Include="Math">
+      <UniqueIdentifier>{fbf12c9c-2f59-42b1-95a0-86b3f59552ea}</UniqueIdentifier>
+    </Filter>
+    <Filter Include="Util">
+      <UniqueIdentifier>{f372a643-077f-44dc-93b7-4aad75c64568}</UniqueIdentifier>
+    </Filter>
+    <Filter Include="Math\Header Files">
+      <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
+      <Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
+    </Filter>
+    <Filter Include="Math\Source Files">
+      <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
+      <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
+    </Filter>
+    <Filter Include="Util\Header Files">
+      <UniqueIdentifier>{b3aa59c7-553c-45b8-b8f8-50802da4ec0e}</UniqueIdentifier>
+    </Filter>
+    <Filter Include="Util\Source Files">
+      <UniqueIdentifier>{2a2a2460-0605-42b0-b7a6-53592bcd5143}</UniqueIdentifier>
+    </Filter>
+  </ItemGroup>
+  <ItemGroup>
+    <ClInclude Include="..\..\Math\Geometry.h">
+      <Filter>Math\Header Files</Filter>
+    </ClInclude>
+    <ClInclude Include="..\..\Math\Vector3.h">
+      <Filter>Math\Header Files</Filter>
+    </ClInclude>
+    <ClInclude Include="..\..\Util\NumericStrings.h">
+      <Filter>Util\Header Files</Filter>
+    </ClInclude>
+  </ItemGroup>
+  <ItemGroup>
+    <ClCompile Include="..\..\Math\Geometry.cpp">
+      <Filter>Math\Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="..\..\Math\Vector3.cpp">
+      <Filter>Math\Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="..\..\Util\NumericStrings.cpp">
+      <Filter>Util\Source Files</Filter>
+    </ClCompile>
+  </ItemGroup>
+</Project>