Open source Star Ruler 2 source code!
This commit is contained in:
@@ -0,0 +1,182 @@
|
||||
#ifndef VECTOR_H
|
||||
#define VECTOR_H 1
|
||||
|
||||
#include <math.h>
|
||||
|
||||
namespace BiPatch {
|
||||
|
||||
class Vector {
|
||||
double d[3];
|
||||
|
||||
public:
|
||||
|
||||
inline Vector (double x, double y, double z=0);
|
||||
inline Vector(const Vector& v);
|
||||
inline Vector();
|
||||
inline double length() const;
|
||||
inline double length2() const;
|
||||
inline Vector& operator=(const Vector& v);
|
||||
inline bool operator==(const Vector& v) const;
|
||||
inline Vector operator*(double s) const;
|
||||
inline Vector operator*(const Vector& v) const;
|
||||
inline Vector operator/(const Vector& v) const;
|
||||
inline Vector& operator*=(double s);
|
||||
Vector operator/(double s) const;
|
||||
inline Vector operator+(const Vector& v) const;
|
||||
inline Vector& operator+=(const Vector& v);
|
||||
inline Vector operator-() const;
|
||||
inline Vector operator-(const Vector& v) const;
|
||||
Vector& operator-=(const Vector& v);
|
||||
inline double normalize();
|
||||
Vector normal() const;
|
||||
inline Vector cross(const Vector& v) const;
|
||||
inline double dot(const Vector& v) const;
|
||||
inline void x(double xx) { d[0]=xx; }
|
||||
inline double x() const;
|
||||
inline void y(double yy) { d[1]=yy; }
|
||||
inline double y() const;
|
||||
inline void z(double zz) { d[2]=zz; }
|
||||
inline double z() const;
|
||||
inline double minComponent() const;
|
||||
inline bool operator != (const Vector& v) const;
|
||||
inline double* ptr() const {return (double*)&d[0];}
|
||||
|
||||
void make_ortho(Vector&v1, Vector&v2)
|
||||
{
|
||||
Vector v0(this->cross(Vector(1,0,0)));
|
||||
if(v0.length2() == 0){
|
||||
v0=this->cross(this->cross(Vector(0,1,0)));
|
||||
}
|
||||
v1=this->cross(v0);
|
||||
v1.normalize();
|
||||
v2=this->cross(v1);
|
||||
v2.normalize();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
inline Vector::Vector(double x, double y, double z) {
|
||||
d[0]=x;
|
||||
d[1]=y;
|
||||
d[2]=z;
|
||||
}
|
||||
|
||||
inline Vector::Vector(const Vector& v) {
|
||||
d[0]=v.d[0];
|
||||
d[1]=v.d[1];
|
||||
d[2]=v.d[2];
|
||||
}
|
||||
|
||||
inline Vector::Vector() {
|
||||
}
|
||||
|
||||
|
||||
inline double Vector::length() const {
|
||||
return sqrt(length2());
|
||||
}
|
||||
|
||||
inline double Vector::length2() const {
|
||||
return d[0]*d[0]+d[1]*d[1]+d[2]*d[2];
|
||||
}
|
||||
|
||||
inline Vector& Vector::operator=(const Vector& v) {
|
||||
d[0]=v.d[0];
|
||||
d[1]=v.d[1];
|
||||
d[2]=v.d[2];
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
inline Vector Vector::operator*(double s) const {
|
||||
return Vector(d[0]*s, d[1]*s, d[2]*s);
|
||||
}
|
||||
|
||||
inline Vector operator*(double s, const Vector& v) {
|
||||
return v*s;
|
||||
}
|
||||
|
||||
inline Vector Vector::operator*(const Vector& v) const {
|
||||
return Vector(d[0]*v.d[0], d[1]*v.d[1], d[2]*v.d[2]);
|
||||
}
|
||||
|
||||
inline Vector Vector::operator/(const Vector& v) const {
|
||||
return Vector(d[0]/v.d[0], d[1]/v.d[1], d[2]/v.d[2]);
|
||||
}
|
||||
|
||||
inline Vector Vector::operator+(const Vector& v) const {
|
||||
return Vector(d[0]+v.d[0], d[1]+v.d[1], d[2]+v.d[2]);
|
||||
}
|
||||
|
||||
inline Vector& Vector::operator+=(const Vector& v) {
|
||||
d[0]+=v.d[0];
|
||||
d[1]+=v.d[1];
|
||||
d[2]+=v.d[2];
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Vector& Vector::operator*=(double s) {
|
||||
d[0]*=s;
|
||||
d[1]*=s;
|
||||
d[2]*=s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Vector Vector::operator-() const {
|
||||
return Vector(-d[0], -d[1], -d[2]);
|
||||
}
|
||||
|
||||
inline Vector Vector::operator-(const Vector& v) const {
|
||||
return Vector(d[0]-v.d[0], d[1]-v.d[1], d[2]-v.d[2]);
|
||||
}
|
||||
|
||||
|
||||
inline double Vector::normalize() {
|
||||
double l=length();
|
||||
if(l != 0)
|
||||
{
|
||||
d[0]/=l;
|
||||
d[1]/=l;
|
||||
d[2]/=l;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
inline Vector Vector::cross(const Vector& v) const {
|
||||
return Vector(d[1]*v.d[2]-d[2]*v.d[1],
|
||||
d[2]*v.d[0]-d[0]*v.d[2],
|
||||
d[0]*v.d[1]-d[1]*v.d[0]);
|
||||
}
|
||||
|
||||
inline double Vector::dot(const Vector& v) const {
|
||||
return d[0]*v.d[0]+d[1]*v.d[1]+d[2]*v.d[2];
|
||||
}
|
||||
|
||||
|
||||
inline double Vector::x() const {
|
||||
return d[0];
|
||||
}
|
||||
|
||||
inline double Vector::y() const {
|
||||
return d[1];
|
||||
}
|
||||
|
||||
inline double Vector::z() const {
|
||||
return d[2];
|
||||
}
|
||||
|
||||
inline double Vector::minComponent() const {
|
||||
return (d[0]<d[1] && d[0]<d[2])?d[0]:d[1]<d[2]?d[1]:d[2];
|
||||
}
|
||||
|
||||
inline bool Vector::operator != (const Vector& v) const {
|
||||
return d[0] != v.d[0] || d[1] != v.d[1] || d[2] != v.d[2];
|
||||
}
|
||||
|
||||
inline bool Vector::operator == (const Vector& v) const {
|
||||
return d[0] == v.d[0] && d[1] == v.d[1] && d[2] == v.d[2];
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,80 @@
|
||||
//created by Shaun David Ramsey and Kristin Potter copyright (c) 2003
|
||||
//email ramsey()cs.utah.edu with any quesitons
|
||||
/*
|
||||
This copyright notice is available at:
|
||||
http://www.opensource.org/licenses/mit-license.php
|
||||
|
||||
Copyright (c) 2003 Shaun David Ramsey, Kristin Potter, Charles Hansen
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sel copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
#include "Vector.h"
|
||||
#define ray_epsilon 1e-4 // some small epsilon for flt pt
|
||||
//#define twoplanes true // comment out this line to use raypatch
|
||||
#ifndef twoplanes // if we're not using patch-twoplanes intersection
|
||||
#define raypatch true //then use ray-patch intersections
|
||||
#endif
|
||||
|
||||
#ifndef BILINEAR_H
|
||||
#define BILINEAR_H
|
||||
|
||||
|
||||
namespace BiPatch {
|
||||
|
||||
//find roots of ax^2+bx+c=0 in the interval min,max.
|
||||
// place the roots in u[2] and return how many roots found
|
||||
int QuadraticRoot(double a, double b, double c,
|
||||
double min, double max,double *u);
|
||||
|
||||
// Bilinear patch class
|
||||
class BilinearPatch
|
||||
{
|
||||
// The four points defining the patch
|
||||
Vector P00, P01, P10, P11;
|
||||
|
||||
public:
|
||||
|
||||
// Constructors
|
||||
BilinearPatch(Vector Pt00, Vector Pt01, Vector Pt10, Vector Pt11);
|
||||
// Destructor
|
||||
~BilinearPatch(){}
|
||||
Vector getP00(){return P00;}
|
||||
// Return the point P01
|
||||
Vector getP01(){return P01;}
|
||||
// Return the point P10
|
||||
Vector getP10(){return P10;}
|
||||
// Return the point P11
|
||||
Vector getP11(){return P11;}
|
||||
// Find the tangent (du)
|
||||
Vector TanU( double v);
|
||||
// Find the tangent (dv)
|
||||
Vector TanV( double u);
|
||||
// Find dudv
|
||||
Vector Normal( double u, double v);
|
||||
// Evaluate the surface of the patch at u,v
|
||||
Vector SrfEval( double u, double v);
|
||||
// Find the local closest point to spacept
|
||||
bool RayPatchIntersection( Vector r, Vector d, Vector &uv);
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user