To have the 50% faster nearest_surface point.
Changed mesh_faces_nearest_point to return the face normal instead of collision normal
This commit is contained in:
2008-08-13 19:22:35 +00:00
parent 6a8236a8da
commit 43bf03580f
8 changed files with 1046 additions and 457 deletions

View File

@@ -48,9 +48,6 @@
/* Math stuff for ray casting on mesh faces and for nearest surface */
static float nearest_point_in_tri_surface(const float *point, const float *v0, const float *v1, const float *v2, float *nearest);
#define ISECT_EPSILON 1e-6
static float ray_tri_intersection(const BVHTreeRay *ray, const float m_dist, const float *v0, const float *v1, const float *v2)
{
float dist;
@@ -79,170 +76,324 @@ static float sphereray_tri_intersection(const BVHTreeRay *ray, float radius, con
return FLT_MAX;
}
/*
* This calculates the distance from point to the plane
* Distance is negative if point is on the back side of plane
* Function adapted from David Eberly's distance tools (LGPL)
* http://www.geometrictools.com/LibFoundation/Distance/Distance.html
*/
static float point_plane_distance(const float *point, const float *plane_point, const float *plane_normal)
static float nearest_point_in_tri_surface(const float *v0,const float *v1,const float *v2,const float *p, int *v, int *e, float *nearest )
{
float pp[3];
VECSUB(pp, point, plane_point);
return INPR(pp, plane_normal);
}
static float choose_nearest(const float v0[2], const float v1[2], const float point[2], float closest[2])
{
float d[2][2], sdist[2];
VECSUB2D(d[0], v0, point);
VECSUB2D(d[1], v1, point);
float diff[3];
float e0[3];
float e1[3];
float A00;
float A01;
float A11;
float B0;
float B1;
float C;
float Det;
float S;
float T;
float sqrDist;
int lv = -1, le = -1;
VECSUB(diff, v0, p);
VECSUB(e0, v1, v0);
VECSUB(e1, v2, v0);
A00 = INPR ( e0, e0 );
A01 = INPR( e0, e1 );
A11 = INPR ( e1, e1 );
B0 = INPR( diff, e0 );
B1 = INPR( diff, e1 );
C = INPR( diff, diff );
Det = fabs( A00 * A11 - A01 * A01 );
S = A01 * B1 - A11 * B0;
T = A01 * B0 - A00 * B1;
sdist[0] = d[0][0]*d[0][0] + d[0][1]*d[0][1];
sdist[1] = d[1][0]*d[1][0] + d[1][1]*d[1][1];
if(sdist[0] < sdist[1])
if ( S + T <= Det )
{
if(closest)
VECCOPY2D(closest, v0);
return sdist[0];
if ( S < 0.0f )
{
if ( T < 0.0f ) // Region 4
{
if ( B0 < 0.0f )
{
T = 0.0f;
if ( -B0 >= A00 )
{
S = (float)1.0;
sqrDist = A00 + 2.0f * B0 + C;
lv = 1;
}
else
{
if(fabs(A00) > FLT_EPSILON)
S = -B0/A00;
else
S = 0.0f;
sqrDist = B0 * S + C;
le = 0;
}
}
else
{
S = 0.0f;
if ( B1 >= 0.0f )
{
T = 0.0f;
sqrDist = C;
lv = 0;
}
else if ( -B1 >= A11 )
{
T = 1.0f;
sqrDist = A11 + 2.0f * B1 + C;
lv = 2;
}
else
{
if(fabs(A11) > FLT_EPSILON)
T = -B1 / A11;
else
T = 0.0f;
sqrDist = B1 * T + C;
le = 1;
}
}
}
else // Region 3
{
S = 0.0f;
if ( B1 >= 0.0f )
{
T = 0.0f;
sqrDist = C;
lv = 0;
}
else if ( -B1 >= A11 )
{
T = 1.0f;
sqrDist = A11 + 2.0f * B1 + C;
lv = 2;
}
else
{
if(fabs(A11) > FLT_EPSILON)
T = -B1 / A11;
else
T = 0.0;
sqrDist = B1 * T + C;
le = 1;
}
}
}
else if ( T < 0.0f ) // Region 5
{
T = 0.0f;
if ( B0 >= 0.0f )
{
S = 0.0f;
sqrDist = C;
lv = 0;
}
else if ( -B0 >= A00 )
{
S = 1.0f;
sqrDist = A00 + 2.0f * B0 + C;
lv = 1;
}
else
{
if(fabs(A00) > FLT_EPSILON)
S = -B0 / A00;
else
S = 0.0f;
sqrDist = B0 * S + C;
le = 0;
}
}
else // Region 0
{
// Minimum at interior lv
float invDet;
if(fabs(Det) > FLT_EPSILON)
invDet = 1.0f / Det;
else
invDet = 0.0f;
S *= invDet;
T *= invDet;
sqrDist = S * ( A00 * S + A01 * T + 2.0f * B0) +
T * ( A01 * S + A11 * T + 2.0f * B1 ) + C;
}
}
else
{
if(closest)
VECCOPY2D(closest, v1);
return sdist[1];
}
}
/*
* calculates the closest point between point-tri (2D)
* returns that tri must be right-handed
* Returns square distance
*/
static float closest_point_in_tri2D(const float point[2], /*const*/ float tri[3][2], float closest[2])
{
float edge_di[2];
float v_point[2];
float proj[2]; //point projected over edge-dir, edge-normal (witouth normalized edge)
const float *v0 = tri[2], *v1;
float edge_slen, d; //edge squared length
int i;
const float *nearest_vertex = NULL;
float tmp0, tmp1, numer, denom;
//for each edge
for(i=0, v0=tri[2], v1=tri[0]; i < 3; v0=tri[i++], v1=tri[i])
{
VECSUB2D(edge_di, v1, v0);
VECSUB2D(v_point, point, v0);
proj[1] = v_point[0]*edge_di[1] - v_point[1]*edge_di[0]; //dot product with edge normal
//point inside this edge
if(proj[1] < 0)
continue;
proj[0] = v_point[0]*edge_di[0] + v_point[1]*edge_di[1];
//closest to this edge is v0
if(proj[0] < 0)
if ( S < 0.0f ) // Region 2
{
if(nearest_vertex == NULL || nearest_vertex == v0)
nearest_vertex = v0;
tmp0 = A01 + B0;
tmp1 = A11 + B1;
if ( tmp1 > tmp0 )
{
numer = tmp1 - tmp0;
denom = A00 - 2.0f * A01 + A11;
if ( numer >= denom )
{
S = 1.0f;
T = 0.0f;
sqrDist = A00 + 2.0f * B0 + C;
lv = 1;
}
else
{
if(fabs(denom) > FLT_EPSILON)
S = numer / denom;
else
S = 0.0f;
T = 1.0f - S;
sqrDist = S * ( A00 * S + A01 * T + 2.0f * B0 ) +
T * ( A01 * S + A11 * T + 2.0f * B1 ) + C;
le = 2;
}
}
else
{
//choose nearest
return choose_nearest(nearest_vertex, v0, point, closest);
S = 0.0f;
if ( tmp1 <= 0.0f )
{
T = 1.0f;
sqrDist = A11 + 2.0f * B1 + C;
lv = 2;
}
else if ( B1 >= 0.0f )
{
T = 0.0f;
sqrDist = C;
lv = 0;
}
else
{
if(fabs(A11) > FLT_EPSILON)
T = -B1 / A11;
else
T = 0.0f;
sqrDist = B1 * T + C;
le = 1;
}
}
i++; //We can skip next edge
continue;
}
edge_slen = edge_di[0]*edge_di[0] + edge_di[1]*edge_di[1]; //squared edge len
//closest to this edge is v1
if(proj[0] > edge_slen)
else if ( T < 0.0f ) // Region 6
{
if(nearest_vertex == NULL || nearest_vertex == v1)
nearest_vertex = v1;
tmp0 = A01 + B1;
tmp1 = A00 + B0;
if ( tmp1 > tmp0 )
{
numer = tmp1 - tmp0;
denom = A00 - 2.0f * A01 + A11;
if ( numer >= denom )
{
T = 1.0f;
S = 0.0f;
sqrDist = A11 + 2.0f * B1 + C;
lv = 2;
}
else
{
if(fabs(denom) > FLT_EPSILON)
T = numer / denom;
else
T = 0.0f;
S = 1.0f - T;
sqrDist = S * ( A00 * S + A01 * T + 2.0f * B0 ) +
T * ( A01 * S + A11 * T + 2.0f * B1 ) + C;
le = 2;
}
}
else
{
return choose_nearest(nearest_vertex, v1, point, closest);
T = 0.0f;
if ( tmp1 <= 0.0f )
{
S = 1.0f;
sqrDist = A00 + 2.0f * B0 + C;
lv = 1;
}
else if ( B0 >= 0.0f )
{
S = 0.0f;
sqrDist = C;
lv = 0;
}
else
{
if(fabs(A00) > FLT_EPSILON)
S = -B0 / A00;
else
S = 0.0f;
sqrDist = B0 * S + C;
le = 0;
}
}
}
else // Region 1
{
numer = A11 + B1 - A01 - B0;
if ( numer <= 0.0f )
{
S = 0.0f;
T = 1.0f;
sqrDist = A11 + 2.0f * B1 + C;
lv = 2;
}
else
{
denom = A00 - 2.0f * A01 + A11;
if ( numer >= denom )
{
S = 1.0f;
T = 0.0f;
sqrDist = A00 + 2.0f * B0 + C;
lv = 1;
}
else
{
if(fabs(denom) > FLT_EPSILON)
S = numer / denom;
else
S = 0.0f;
T = 1.0f - S;
sqrDist = S * ( A00 * S + A01 * T + 2.0f * B0 ) +
T * ( A01 * S + A11 * T + 2.0f * B1 ) + C;
le = 2;
}
}
continue;
}
//nearest is on this edge
d= proj[1] / edge_slen;
closest[0] = point[0] - edge_di[1] * d;
closest[1] = point[1] + edge_di[0] * d;
return proj[1]*proj[1]/edge_slen;
}
if(nearest_vertex)
// Account for numerical round-off error
if ( sqrDist < FLT_EPSILON )
sqrDist = 0.0f;
{
VECSUB2D(v_point, nearest_vertex, point);
VECCOPY2D(closest, nearest_vertex);
return v_point[0]*v_point[0] + v_point[1]*v_point[1];
float w[3], x[3], y[3], z[3];
VECCOPY(w, v0);
VECCOPY(x, e0);
VecMulf(x, S);
VECCOPY(y, e1);
VecMulf(y, T);
VECADD(z, w, x);
VECADD(z, z, y);
//VECSUB(d, p, z);
VECCOPY(nearest, z);
// d = p - ( v0 + S * e0 + T * e1 );
}
else
{
VECCOPY(closest, point); //point is already inside
return 0.0f;
}
}
*v = lv;
*e = le;
/*
* Returns the square of the minimum distance between the point and a triangle surface
* If nearest is not NULL the nearest surface point is written on it
*/
static float nearest_point_in_tri_surface(const float *point, const float *v0, const float *v1, const float *v2, float *nearest)
{
//Lets solve the 2D problem (closest point-tri)
float normal_dist, plane_sdist, plane_offset;
float du[3], dv[3], dw[3]; //orthogonal axis (du=(v0->v1), dw=plane normal)
float p_2d[2], tri_2d[3][2], nearest_2d[2];
CalcNormFloat((float*)v0, (float*)v1, (float*)v2, dw);
//point-plane distance and calculate axis
normal_dist = point_plane_distance(point, v0, dw);
// OPTIMIZATION
// if we are only interested in nearest distance if its closer than some distance already found
// we can:
// if(normal_dist*normal_dist >= best_dist_so_far) return FLOAT_MAX;
//
VECSUB(du, v1, v0);
Normalize(du);
Crossf(dv, dw, du);
plane_offset = INPR(v0, dw);
//project stuff to 2d
tri_2d[0][0] = INPR(du, v0);
tri_2d[0][1] = INPR(dv, v0);
tri_2d[1][0] = INPR(du, v1);
tri_2d[1][1] = INPR(dv, v1);
tri_2d[2][0] = INPR(du, v2);
tri_2d[2][1] = INPR(dv, v2);
p_2d[0] = INPR(du, point);
p_2d[1] = INPR(dv, point);
//we always have a right-handed tri
//this should always happen because of the way normal is calculated
plane_sdist = closest_point_in_tri2D(p_2d, tri_2d, nearest_2d);
//project back to 3d
if(nearest)
{
nearest[0] = du[0]*nearest_2d[0] + dv[0] * nearest_2d[1] + dw[0] * plane_offset;
nearest[1] = du[1]*nearest_2d[0] + dv[1] * nearest_2d[1] + dw[1] * plane_offset;
nearest[2] = du[2]*nearest_2d[0] + dv[2] * nearest_2d[1] + dw[2] * plane_offset;
}
return plane_sdist + normal_dist*normal_dist;
return sqrDist;
}
@@ -268,22 +419,15 @@ static void mesh_faces_nearest_point(void *userdata, int index, const float *co,
do
{
float nearest_tmp[3], dist;
float vec[3][3];
int vertex, edge;
// only insert valid triangles / quads with area > 0
VECSUB(vec[0], t2, t1);
VECSUB(vec[1], t0, t1);
Crossf(vec[2], vec[0], vec[1]);
if(INPR(vec[2], vec[2]) >= FLT_EPSILON)
dist = nearest_point_in_tri_surface(t0, t1, t2, co, &vertex, &edge, nearest_tmp);
if(dist < nearest->dist)
{
dist = nearest_point_in_tri_surface(co,t0, t1, t2, nearest_tmp);
if(dist < nearest->dist)
{
nearest->index = index;
nearest->dist = dist;
VECCOPY(nearest->co, nearest_tmp);
CalcNormFloat((float*)t0, (float*)t1, (float*)t2, nearest->no); //TODO.. (interpolate normals from the vertexs coordinates?
}
nearest->index = index;
nearest->dist = dist;
VECCOPY(nearest->co, nearest_tmp);
CalcNormFloat(t0, t1, t2, nearest->no);
}
t1 = t2;

View File

@@ -405,7 +405,7 @@ void shrinkwrap_calc_normal_projection(ShrinkwrapCalcData *calc)
//Now, everything is ready to project the vertexs!
//#pragma omp parallel for private(i,hit) schedule(static)
#pragma omp parallel for private(i,hit) schedule(static)
for(i = 0; i<calc->numVerts; ++i)
{
float *co = calc->vertexCos[i];

File diff suppressed because it is too large Load Diff

View File

@@ -105,6 +105,10 @@
#define EXPP_TEX_LACUNARITY_MAX 6.0f
#define EXPP_TEX_OCTS_MIN 0.0f
#define EXPP_TEX_OCTS_MAX 8.0f
#define EXPP_TEX_OFST_MIN 0.0f
#define EXPP_TEX_OFST_MAX 6.0f
#define EXPP_TEX_GAIN_MIN 0.0f
#define EXPP_TEX_GAIN_MAX 6.0f
#define EXPP_TEX_ISCALE_MIN 0.0f
#define EXPP_TEX_ISCALE_MAX 10.0f
#define EXPP_TEX_EXP_MIN 0.010f
@@ -430,6 +434,8 @@ GETFUNC( getNoiseDepth );
GETFUNC( getNoiseSize );
GETFUNC( getNoiseType );
GETFUNC( getOcts );
GETFUNC( getOffset );
GETFUNC( getGain );
GETFUNC( getRepeat );
GETFUNC( getRGBCol );
GETFUNC( getSType );
@@ -478,6 +484,8 @@ SETFUNC( setNoiseDepth );
SETFUNC( setNoiseSize );
SETFUNC( setNoiseType );
SETFUNC( setOcts );
SETFUNC( setOffset );
SETFUNC( setGain );
SETFUNC( setRepeat );
SETFUNC( setRGBCol );
SETFUNC( setSType );
@@ -646,6 +654,14 @@ static PyGetSetDef BPy_Texture_getseters[] = {
(getter)Texture_getLacunarity, (setter)Texture_setLacunarity,
"Gap between succesive frequencies (for Musgrave textures)",
NULL},
{"offset",
(getter)Texture_getOffset, (setter)Texture_setOffset,
"Fractal offset (for Musgrave textures)",
NULL},
{"gain",
(getter)Texture_getGain, (setter)Texture_setGain,
"Gain multiplier (for Musgrave textures)",
NULL},
{"noiseBasis",
(getter)Texture_getNoiseBasis, (setter)Texture_setNoiseBasis,
"Noise basis type (wood, stucci, marble, clouds, Musgrave, distorted noise)",
@@ -1837,6 +1853,20 @@ static int Texture_setLacunarity( BPy_Texture * self, PyObject * value )
EXPP_TEX_LACUNARITY_MAX );
}
static int Texture_setOffset( BPy_Texture * self, PyObject * value )
{
return EXPP_setFloatClamped ( value, &self->texture->mg_offset,
EXPP_TEX_OFST_MIN,
EXPP_TEX_OFST_MAX );
}
static int Texture_setGain( BPy_Texture * self, PyObject * value )
{
return EXPP_setFloatClamped ( value, &self->texture->mg_gain,
EXPP_TEX_GAIN_MIN,
EXPP_TEX_GAIN_MAX );
}
static int Texture_setOcts( BPy_Texture * self, PyObject * value )
{
return EXPP_setFloatClamped ( value, &self->texture->mg_octaves,
@@ -2168,6 +2198,16 @@ static PyObject *Texture_getOcts( BPy_Texture *self )
return PyFloat_FromDouble( self->texture->mg_octaves );
}
static PyObject *Texture_getOffset( BPy_Texture *self )
{
return PyFloat_FromDouble( self->texture->mg_offset );
}
static PyObject *Texture_getGain( BPy_Texture *self )
{
return PyFloat_FromDouble( self->texture->mg_gain );
}
static PyObject *Texture_getRepeat( BPy_Texture *self )
{
return Py_BuildValue( "(i,i)", self->texture->xrepeat,

View File

@@ -344,6 +344,12 @@ class Texture:
@ivar octs: Number of frequencies (for Musgrave textures).
Value is clamped to the range [0.0,8.0].
@type octs: float
@ivar offset: Fractal offset (for hetero terrain and multifractal Musgrave textures).
Value is clamped to the range [0.0,6.0].
@type offset: float
@ivar gain: Gain multiplier (for multifractal Musgrave textures).
Value is clamped to the range [0.0,6.0].
@type gain: float
@ivar repeat: Repetition multiplier (for image textures).
@type repeat: tuple of 2 ints
@ivar rgbCol: RGB color tuple.

View File

@@ -6720,3 +6720,4 @@ void editing_panels()
}
uiClearButLock();
}

View File

@@ -700,9 +700,7 @@ CValue* CValue::ConvertPythonToValue(PyObject* pyobj)
CValue* vallie = NULL;
PyTypeObject* type = pyobj->ob_type;
if (type == &PyList_Type)
if (PyList_Check(pyobj))
{
CListValue* listval = new CListValue();
bool error = false;
@@ -732,26 +730,25 @@ CValue* CValue::ConvertPythonToValue(PyObject* pyobj)
}
} else
if (type == &PyFloat_Type)
if (PyFloat_Check(pyobj))
{
float fl;
PyArg_Parse(pyobj,"f",&fl);
vallie = new CFloatValue(fl);
vallie = new CFloatValue( (float)PyFloat_AsDouble(pyobj) );
} else
if (type==&PyInt_Type)
if (PyInt_Check(pyobj))
{
int innie;
PyArg_Parse(pyobj,"i",&innie);
vallie = new CIntValue(innie);
vallie = new CIntValue( (int)PyInt_AS_LONG(pyobj) );
} else
if (type==&PyString_Type)
if (PyString_Check(pyobj))
{
vallie = new CStringValue(PyString_AsString(pyobj),"");
} else
if (type==&CValue::Type || type==&CListValue::Type)
if (pyobj->ob_type==&CValue::Type || pyobj->ob_type==&CListValue::Type)
{
vallie = ((CValue*) pyobj)->AddRef();
} else
{
/* return an error value from the caller */
PyErr_SetString(PyExc_TypeError, "This python value could not be assigned to a game engine property");
}
return vallie;
@@ -778,6 +775,9 @@ int CValue::_setattr(const STR_String& attr,PyObject* pyobj)
SetProperty(attr,vallie);
}
vallie->Release();
} else
{
return 1; /* ConvertPythonToValue sets the error message */
}
//PyObjectPlus::_setattr(attr,value);

View File

@@ -273,36 +273,16 @@ void SCA_PythonController::Trigger(SCA_LogicManager* logicmgr)
* break it by hand, then DECREF (which in this case
* should always ensure excdict is cleared).
*/
/* PyObject *excdict= myPyDict_Copy(m_pythondictionary);
struct _object* resultobj = PyEval_EvalCode((PyCodeObject*)m_bytecode,
excdict,
excdict
);
PyDict_Clear(excdict);
Py_DECREF(excdict);*/
#if 1
PyObject *excdict= PyDict_Copy(m_pythondictionary);
PyObject* resultobj = PyEval_EvalCode((PyCodeObject*)m_bytecode,
excdict,
excdict
);
PyDict_Clear(excdict);
Py_DECREF(excdict);
#else
PyObject* resultobj = PyEval_EvalCode((PyCodeObject*)m_bytecode,
m_pythondictionary,
m_pythondictionary
);
#endif
excdict, excdict);
if (resultobj)
{
Py_DECREF(resultobj);
} else
}
else
{
// something is wrong, tell the user what went wrong
printf("PYTHON SCRIPT ERROR:\n");
@@ -310,6 +290,11 @@ void SCA_PythonController::Trigger(SCA_LogicManager* logicmgr)
//PyRun_SimpleString(m_scriptText.Ptr());
}
// clear after PyErrPrint - seems it can be using
// something in this dictionary and crash?
PyDict_Clear(excdict);
Py_DECREF(excdict);
m_sCurrentController = NULL;
}