Skip to content

Commit

Permalink
Update fastvoxel, ply compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-f committed Jun 22, 2017
1 parent 13c986e commit 2cc8f28
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 198 deletions.
177 changes: 102 additions & 75 deletions src/Core/mathlib.h
Original file line number Diff line number Diff line change
@@ -1,47 +1,34 @@
/***************************************************************************
* Mathlib
*
* Copyright (C) 2003-2004, Alexander Zaprjagaev <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
***************************************************************************
* Update 2004/08/19
*
* added ivec2, ivec3 & ivec4 methods
* vec2, vec3 & vec4 data : added texture coords (s,t,p,q) and color enums (r,g,b,a)
* mat3 & mat4 : added multiple float constructor ad modified methods returning mat3 or mat4
* optimisations like "x / 2.0f" replaced by faster "x * 0.5f"
* defines of multiples usefull maths values and radian/degree conversions
* vec2 : added methods : set, reset, compare, dot, closestPointOnLine, closestPointOnSegment,
* projectionOnLine, lerp, angle
* vec3 : added methods : set, reset, compare, dot, cross, closestPointOnLine, closestPointOnSegment,
* projectionOnLine, lerp, angle
* vec4 : added methods : set, reset, compare
***************************************************************************
*/


/**
* 2011 Edited, add some function and typedef, templates
* FastVoxel is a voxelisation library of polygonal 3d model and do volumes identifications.
* It is dedicated to finite element solvers
* @author Nicolas Fortin , Judicael Picaut judicael.picaut (home) ifsttar.fr
* Official repository is https://github.com/nicolas-f/FastVoxel
*/
#include "std_tools.hpp"
/* ----------------------------------------------------------------------
* I-SIMPA (http://i-simpa.ifsttar.fr). This file is part of I-SIMPA.
*
* I-SIMPA is a GUI for 3D numerical sound propagation modelling dedicated
* to scientific acoustic simulations.
* Copyright (C) 2007-2014 - IFSTTAR - Judicael Picaut, Nicolas Fortin
*
* I-SIMPA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* I-SIMPA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or
* see <http://ww.gnu.org/licenses/>
*
* For more information, please consult: <http://i-simpa.ifsttar.fr> or
* send an email to [email protected]
*
* To contact Ifsttar, write to Ifsttar, 14-20 Boulevard Newton
* Cite Descartes, Champs sur Marne F-77447 Marne la Vallee Cedex 2 FRANCE
* or write to [email protected]
* ----------------------------------------------------------------------*/

#include "../std_tools.hpp"
#include <math.h>
#include <stdlib.h>
#include <float.h>
Expand All @@ -55,7 +42,12 @@
#ifndef __HMATHLIB__
#define __HMATHLIB__


// Number precision for display (after comma separator)
const int COMMA_PRECISION_DB = 1;
const int COMMA_PRECISION_TIME_S = 2;
const int COMMA_PRECISION_TIME_MS = 1;
const int COMMA_PRECISION_PERCENT = 1;
const int COMMA_PRECISION_AREA = 2;


/**
Expand Down Expand Up @@ -106,12 +98,15 @@ class base_vec3 {
public:
base_vec3(void) : x(0), y(0), z(0) { }
base_vec3(const base_t& _x,const base_t& _y,const base_t& _z) : x(_x), y(_y), z(_z) { }
base_vec3(const base_t *_v) : x(_v[0]), y(_v[1]), z(_v[2]) { }
base_vec3(const double _v[3]) : x(_v[0]), y(_v[1]), z(_v[2]) { }
base_vec3(const float _v[3]) : x(_v[0]), y(_v[1]), z(_v[2]) { }
base_vec3(const vec2 &_v,base_t _z);
base_vec3(const base_vec3 &_v) : x(_v.x), y(_v.y), z(_v.z) { }
base_vec3(const vec4 &_v);

int operator==(const base_vec3 &_v) { return (fabs(this->x - _v.x) < EPSILON && fabs(this->y - _v.y) < EPSILON && fabs(this->z - _v.z) < EPSILON); }
int operator==(const base_vec3 &_v) {
return (fabs(this->x - _v.x) < EPSILON && fabs(this->y - _v.y) < EPSILON && fabs(this->z - _v.z) < EPSILON);
}
int operator!=(const base_vec3 &_v) { return !(*this == _v); }

base_vec3 &operator=(base_t _f) { this->x=_f; this->y=_f; this->z=_f; return (*this); }
Expand All @@ -136,6 +131,16 @@ class base_vec3 {
base_t operator*(const base_vec3 &_v) const { return this->x * _v.x + this->y * _v.y + this->z * _v.z; }
base_t operator*(const vec4 &_v) const;

/**
* Copy this vector into the float array in parameter
* @param arr[out] External array
*/
void copyTo(float* arr) {
arr[0] = x;
arr[1] = y;
arr[2] = z;
}

operator base_t*() { return this->v; }
operator const base_t*() const { return this->v; }
base_t &operator[](int _i) { return this->v[_i]; }
Expand Down Expand Up @@ -238,6 +243,15 @@ class base_vec3 {
typedef base_vec3<decimal> vec3;
typedef base_vec3<double> dvec3;


inline vec3 dvec3_to_vec3(const dvec3 &v1) {
return vec3(v1.x, v1.y, v1.z);
}

inline dvec3 vec3_to_dvec3(const vec3 &v1) {
return dvec3(v1.x, v1.y, v1.z);
}

/*****************************************************************************/
/* */
/* vec2 */
Expand Down Expand Up @@ -329,8 +343,10 @@ inline void Cross(const vec3 &v1,const vec3 &v2,vec3 &vout) {
vout.y=v1.z * v2.x - v1.x * v2.z;
vout.z=v1.x * v2.y - v1.y * v2.x;
}
inline vec3 Cross_r(const vec3 &v1,const vec3 &v2) {
return vec3(v1.y * v2.z - v1.z * v2.y,v1.z * v2.x - v1.x * v2.z,v1.x * v2.y - v1.y * v2.x);

template<typename base_t>
inline base_vec3<base_t> Cross_r(const base_vec3<base_t> &v1,const base_vec3<base_t> &v2) {
return base_vec3<base_t>(v1.y * v2.z - v1.z * v2.y,v1.z * v2.x - v1.x * v2.z,v1.x * v2.y - v1.y * v2.x);
}
template<typename base_t>
inline base_vec3<base_t>::base_vec3(const vec2 &_v,base_t _z)
Expand All @@ -344,15 +360,18 @@ inline vec2::vec2(const vec3 &_v) {
}

// This calculates a vector between 2 points and returns the result
inline void Vector(const vec3 &vp1, const vec3 &vp2,vec3 &vout) {
template<typename base_t>
inline void Vector(const base_vec3<base_t> &vp1, const base_vec3<base_t> &vp2, base_vec3<base_t> &vout) {

vout.x=vp1.x - vp2.x;
vout.y=vp1.y - vp2.y;
vout.z=vp1.z - vp2.z;
}
inline vec3 Vector_r(const vec3 &vp1, const vec3 &vp2)

template<typename base_t>
inline base_vec3<base_t> Vector_r(const base_vec3<base_t> &vp1, const base_vec3<base_t> &vp2)
{
return vec3(vp1.x - vp2.x,vp1.y - vp2.y,vp1.z - vp2.z);
return base_vec3<base_t>(vp1.x - vp2.x,vp1.y - vp2.y,vp1.z - vp2.z);
}
/**
* This calculates determinant 3*3
Expand All @@ -372,10 +391,11 @@ inline decimal Determinant(const vec3 &vp1, const vec3 &vp2, const vec3 &vp3) {
/* */
/*****************************************************************************/

inline vec3 FaceNormal(const vec3 &vp1, const vec3 &vp2, const vec3 &vp3)
template<typename base_t>
inline base_vec3<base_t> FaceNormal(const base_vec3<base_t> &vp1, const base_vec3<base_t> &vp2, const base_vec3<base_t> &vp3)
{

vec3 vret=Cross_r(Vector_r(vp1, vp2),Vector_r(vp2, vp3));
base_vec3<base_t> vret=Cross_r(Vector_r(vp1, vp2),Vector_r(vp2, vp3));
vret.normalize();
return vret;
}
Expand Down Expand Up @@ -470,12 +490,13 @@ inline vec2::vec2(const vec4 &_v) {
this->x = _v.x;
this->y = _v.y;
}
inline bool colinear( const vec3& A, const vec3& B, const vec3& C, const decimal& aproximation)

template<typename base_t>
inline bool colinear( const base_vec3<base_t>& A, const base_vec3<base_t>& B, const base_vec3<base_t>& C, const decimal& aproximation)
{
vec3 AB=B-A;
vec3 AC=C-A;
decimal diff=(AB/AB.length()-AC/AC.length()).length();
return diff<aproximation;
base_vec3<base_t> AB=B-A;
base_vec3<base_t> AC=C-A;
return (AB / AB.length() - AC / AC.length()).length() < aproximation;
}
/*****************************************************************************/
/* */
Expand Down Expand Up @@ -543,10 +564,13 @@ class ivec3 {
ivec3(void) : a(0), b(0), c(0) { }
ivec3(long _a,long _b,long _c) : a(_a), b(_b), c(_c) { }
ivec3(const long *iv) : a(iv[0]), b(iv[1]), c(iv[2]) { }
ivec3(const int *iv) : a(iv[0]), b(iv[1]), c(iv[2]) { }
ivec3(const ivec3 &iv) : a(iv.a), b(iv.b), c(iv.c) { }
ivec3(const ivec4 &iv);

int operator==(const ivec3 &iv) { return ((this->a == iv.a) && (this->b == iv.b) && (this->c == iv.c)); }
int operator==(const ivec3 &iv) {
return ((this->a == iv.a) && (this->b == iv.b) && (this->c == iv.c));
}
int operator!=(const ivec3 &iv) { return !(*this == iv); }

ivec3 &operator=(long _i) { this->x=_i; this->y=_i; this->z=_i; return (*this); }
Expand All @@ -567,11 +591,10 @@ class ivec3 {

operator long*() { return this->i; }
operator const long*() const { return this->i; }
// long &operator[](int _i) { return this->i[_i]; }
// const long &operator[](int _i) const { return this->i[_i]; }

void set(long _a,long _b,long _c) { this->a = _a; this->b = _b; this->c = _c; }
void set(int tab[3]) { this->a = tab[0]; this->b = tab[1]; this->c = tab[2]; }
void set(int index, const long& value) { i[index] = value; }
void reset(void) { this->a = this->b = this->c = 0; }
void swap(ivec3 &iv) { long tmp=a; a=iv.a; iv.a=tmp; tmp=b; b=iv.b; iv.b=tmp; tmp=c; c=iv.c; iv.c=tmp; }
void swap(ivec3 *iv) { this->swap(*iv); }
Expand Down Expand Up @@ -615,6 +638,7 @@ class ivec4 {
ivec4(void) : a(0), b(0), c(0), d(1) { }
ivec4(long _a,long _b,long _c,long _d) : a(_a), b(_b), c(_c), d(_d) { }
ivec4(const long *iv) : a(iv[0]), b(iv[1]), c(iv[2]), d(iv[3]) { }
ivec4(const int *iv) : a(iv[0]), b(iv[1]), c(iv[2]), d(iv[3]) { }
ivec4(const ivec3 &iv) : a(iv.a), b(iv.b), c(iv.c), d(1) { }
ivec4(const ivec3 &iv,long _d) : a(iv.a), b(iv.b), c(iv.c), d(_d) { }
ivec4(const ivec4 &iv) : a(iv.a), b(iv.b), c(iv.c), d(iv.d) { }
Expand All @@ -639,8 +663,8 @@ class ivec4 {

operator long*() { return this->i; }
operator const long*() const { return this->i; }
// long &operator[](int _i) { return this->i[_i]; }
// const long &operator[](int _i) const { return this->i[_i]; }
long &operator[](int _i) { return this->i[_i]; }
const long &operator[](int _i) const { return this->i[_i]; }

void set(long _a,long _b,long _c,long _d) { this->a = _a; this->b = _b; this->c = _c; this->d = _d; }
void reset(void) { this->a = this->b = this->c = this->d = 0; }
Expand Down Expand Up @@ -676,36 +700,39 @@ inline decimal CalcTetraVolume(vec3 A,vec3 B,vec3 C,vec3 D)
/**
* @return La position du point central d'un triangle
*/
inline vec3 GetGTriangle(const vec3& A,const vec3& B,const vec3& C)
template<typename base_t>
inline base_vec3<base_t> GetGTriangle(const base_vec3<base_t>& A,const base_vec3<base_t>& B,const base_vec3<base_t>& C)
{
vec3 I=(B+C)/2;
vec3 AG=(I-A)*(2.f/3.f);
base_vec3<base_t> I=(B+C)/2;
base_vec3<base_t> AG=(I-A)*(2.f/3.f);
return A+AG;
}
inline vec3 GetGTetra(const vec3& A,const vec3& B,const vec3& C,const vec3& D)
{
return (A+B+C+D)/4;
}
inline decimal GetAireTriangle(const vec3& a,const vec3& b,const vec3& c)
template<typename base_t>
inline decimal GetAireTriangle(const base_vec3<base_t>& a,const base_vec3<base_t>& b,const base_vec3<base_t>& c)
{
vec3 ab;
vec3 ac;
base_vec3<base_t> ab;
base_vec3<base_t> ac;
Vector(a,b,ab);
Vector(a,c,ac);
ab.cross(ac);
return .5f*ab.length();
return .5*ab.length();
}
/*
* @param ecart Au maximum 2*PI au minimum 0
* @return Vrai si Si ecart==0 ou ecart>0.1
*/
inline bool DotIsInVertex(const vec3& dot,const vec3& va,const vec3& vb,const vec3& vc,decimal* ecart=NULL)
template<typename base_t>
inline bool DotIsInVertex(const base_vec3<base_t>& dot,const base_vec3<base_t>& va,const base_vec3<base_t>& vb,const base_vec3<base_t>& vc,decimal* ecart=NULL)
{ //retourne vrai si le point est dans un triangle, le total à la fin correspond à environ 2PI si c'est le cas
decimal totangle=0;
// calcul des vecteurs des cotés
vec3 vda(dot-va);
vec3 vdb(dot-vb);
vec3 vdc(dot-vc);
base_vec3<base_t> vda(dot-va);
base_vec3<base_t> vdb(dot-vb);
base_vec3<base_t> vdc(dot-vc);
//Calcul de la somme des angles sur le plan xy
totangle+=vda.angle(vdb);
totangle+=vda.angle(vdc);
Expand Down
Loading

0 comments on commit 2cc8f28

Please sign in to comment.