Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project done #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# source, object and header files
SRC := $(wildcard src/*.cpp)
OBJ := $(SRC:src/%.cpp=obj/%.o)
HEADER := $(wildcard headers/*.h)

# preprocessor flags
CPPFLAGS := -Iheaders

# compiler flags
CXXFLAGS := -Wall -std=c++17

all : main

main : $(OBJ) obj/main.o
$(CXX) -o $@ $^ $(LDLIBS)

obj/main.o : tests/main.cpp $(HEADERS) | obj
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<

obj/%.o : src/%.cpp $(HEADER) | obj
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<

obj :
mkdir $@

clean :
$(RM) -r obj main

.PHONY : clean all

86 changes: 0 additions & 86 deletions headers/common.h

This file was deleted.

27 changes: 27 additions & 0 deletions headers/figure3d.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef _FIGURE3D_H_
#define _FIGURE3D_H_


#include "point3d.h"
#include <utility>
#include <vector>

using std::vector;
using std::pair;
typedef pair<int, int> pii;

struct Figure3D {
int num_points, num_edges;
vector<Point3D> points;
vector<pii> edges;
Figure3D(): num_points(0), num_edges(0) {}
Figure3D(vector<Point3D> points, vector<pii> edges):
num_points(points.size()), num_edges(edges.size()),
points(points), edges(edges) {}
void turn3D(Angle3D a, Point3D p);
void project(Point3D p);
friend ostream& operator<<(ostream& os, const Figure3D& f);
friend istream& operator>>(istream& is, Figure3D& f);
};

#endif
26 changes: 26 additions & 0 deletions headers/point3d.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef _POINT3D_H_
#define _POINT3D_H_

#include "quaternion.h"
#include <tuple>

typedef std::tuple<double, double, double> Angle3D;

struct Point3D {
double x, y, z;
Point3D from_spherical() const;
Point3D from_cylinder() const;
Point3D to_spherical() const;
Point3D to_cylinder() const;
Point3D operator+(Point3D p2) const;
Point3D operator*(double a) const;
bool operator==(Point3D p2) const;
Point3D turn3D(Quaternion q) const;
Point3D turn3D(Angle3D a, Point3D p) const;
Point3D project(Point3D normal) const;
friend ostream& operator<<(ostream& os, Point3D p);
friend istream& operator>>(istream& is, Point3D& p);
};


#endif
24 changes: 24 additions & 0 deletions headers/quaternion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef _QUATERNION_H_
#define _QUATERNION_H_

#include <iostream>

using std::ostream;
using std::istream;

struct Quaternion {
double s, x, y, z;
Quaternion(): s(0), x(0), y(0), z(0) {}
Quaternion(double s): s(s), x(0), y(0), z(0) {}
Quaternion(double s, double x, double y, double z):
s(s), x(x), y(y), z(z) {}
Quaternion operator+(Quaternion q2) const;
Quaternion operator-(Quaternion q2) const;
Quaternion operator*(Quaternion q2) const;
Quaternion operator/(Quaternion q2) const;
bool operator==(Quaternion q2) const;
friend ostream& operator<<(ostream& os, Quaternion q);
friend istream& operator>>(istream& is, Quaternion& q);
};

#endif
39 changes: 39 additions & 0 deletions src/figure3d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "figure3d.h"
#include <cmath>

void Figure3D::turn3D(Angle3D a, Point3D p) {
for (auto& v : points) {
v = v.turn3D(a, p);
}
}

void Figure3D::project(Point3D normal) {
for (auto& v : points) {
v = v.project(normal);
}
}

ostream& operator<<(ostream& os, const Figure3D& f) {
os << "Points:\n";
for (int i = 0; i < (int)f.points.size(); i++) {
os << i << ": " << f.points[i] << '\n';
}
os << "Edges:\n";
for (auto& e: f.edges) {
os << e.first << " <-> " << e.second << '\n';
}
return os;
}

istream& operator>>(istream& is, Figure3D& f) {
is >> f.num_points >> f.num_edges;
f.points.resize(f.num_points);
f.edges.resize(f.num_edges);
for (int i = 0; i < f.num_points; i++) {
is >> f.points[i];
}
for (int i = 0; i < f.num_edges; i++) {
is >> f.edges[i].first >> f.edges[i].second;
}
return is;
}
85 changes: 85 additions & 0 deletions src/point3d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "point3d.h"
#include <cmath>

static const double EPS = 1e-6;

Point3D Point3D::to_spherical() const {
double r = sqrt(x*x + y*y + z*z);
double phi = atan(y/x);
double theta = acos(z/r);
return {r, phi, theta};
}

Point3D Point3D::from_spherical() const {
double r = this->x;
double phi = this->y;
double theta = this->z;
double x = r*sin(theta)*cos(phi);
double y = r*sin(theta)*sin(phi);
double z = r*cos(theta);
return {x, y, z};
}

Point3D Point3D::from_cylinder() const {
double rho = this->x;
double phi = this->y;
double x = rho*cos(phi);
double y = rho*sin(phi);
return {x, y, z};
}

Point3D Point3D::to_cylinder() const {
double rho = sqrt(x*x + y*y);
double phi = 0;
if (x >= 0) phi = asin(y/rho);
if (x < 0) phi = -asin(y/rho) + M_PI;
return {rho, phi, z};
}

Point3D Point3D::operator+(Point3D p) const {
return {x + p.x, y + p.y, z + p.z};
}

Point3D Point3D::operator*(double a) const {
return {a*x, a*y, a*z};
}

Point3D Point3D::turn3D(Quaternion q) const {
Quaternion v = {0, x, y, z};
Quaternion q_inv = Quaternion(1)/q;
Quaternion r = q*v*q_inv;
return {r.x, r.y, r.z};
}

Point3D Point3D::turn3D(Angle3D a, Point3D p) const {
Point3D result = (*this) + p*(-1);
auto [ax, ay, az] = a;
Quaternion qx = {cos(ax/2), sin(ax/2), 0, 0};
Quaternion qy = {cos(ay/2), 0, sin(ay/2), 0};
Quaternion qz = {cos(az/2), 0, 0, sin(az/2)};
result = result.turn3D(qx).turn3D(qy).turn3D(qz);
return result + p;
}

Point3D Point3D::project(Point3D normal) const {
auto [a, b, c] = normal;
double norm2 = (a*a + b*b + c*c);
double x0 = -(b*(a*y - b*x) + c*(a*z - c*x))/norm2;
double y0 = (a*a*y - a*b*x - b*c*z + c*c*y)/norm2;
double z0 = (a*a*z - a*c*x + b*b*z - b*c*y)/norm2;
return {x0, y0, z0};
}

bool Point3D::operator==(Point3D p) const {
return fabs(x - p.x) < EPS &&
fabs(y - p.y) < EPS &&
fabs(z - p.z) < EPS;
}

ostream& operator<<(ostream& os, Point3D p) {
return os << "x=" << p.x << " y=" << p.y << " z=" << p.z;
}

istream& operator>>(istream& is, Point3D& p) {
return is >> p.x >> p.y >> p.z;
}
46 changes: 46 additions & 0 deletions src/quaternion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "quaternion.h"
#include <cmath>

static const double EPS = 1e-6;

Quaternion Quaternion::operator+(Quaternion q2) const {
return {s + q2.s, x + q2.x, y + q2.y, z + q2.z};
}

Quaternion Quaternion::operator-(Quaternion q2) const {
return {s - q2.s, x - q2.x, y - q2.y, z - q2.z};
}

Quaternion Quaternion::operator*(Quaternion q2) const {
return {s * q2.s - x * q2.x - y * q2.y - z * q2.z,
s * q2.x + q2.s * x + y * q2.z - q2.y * z,
s * q2.y + q2.s * y + q2.x * z - x * q2.z,
s * q2.z + q2.s * z + x * q2.y - q2.x * y};
}

Quaternion Quaternion::operator/(Quaternion q2) const {
Quaternion invQ2;
double norm2 = q2.s * q2.s + q2.x * q2.x + q2.y * q2.y + q2.z * q2.z;
double invNorm2 = 1.0 / norm2;
invQ2.s = q2.s * invNorm2;
invQ2.x = -q2.x * invNorm2;
invQ2.y = -q2.y * invNorm2;
invQ2.z = -q2.z * invNorm2;
return (*this * invQ2);
}

bool Quaternion::operator==(Quaternion q2) const {
return fabs(x - q2.x) < EPS &&
fabs(y - q2.y) < EPS &&
fabs(z - q2.z) < EPS &&
fabs(s - q2.s) < EPS;
}

ostream& operator<<(ostream& os, Quaternion q) {
return os << q.s << " + " << q.x << "i + "
<< q.y << "j + " << q.z << "k";
}

istream& operator>>(istream& is, Quaternion& q) {
return is >> q.s >> q.x >> q.y >> q.z;
}
Loading