1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
// Operator2Expr.cpp: implementation of the COperator2Expr class.
/*
* Copyright (c) 1996-2000 Erwin Coumans <coockie@acm.org>
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Erwin Coumans makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
*/
// 31 dec 1998 - big update: try to use the cached data for updating, instead of
// rebuilding completely it from left and right node. Modified flags and bounding boxes
// have to do the trick
// when expression is cached, there will be a call to UpdateCalc() instead of Calc()
#include "Operator2Expr.h"
#include "StringValue.h"
#include "VoidValue.h"
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
COperator2Expr::COperator2Expr(VALUE_OPERATOR op, CExpression *lhs, CExpression *rhs)
:
m_rhs(rhs),
m_lhs(lhs),
m_cached_calculate(NULL),
m_op(op)
/*
pre:
effect: constucts a COperator2Expr with op, lhs and rhs in it
*/
{
}
COperator2Expr::COperator2Expr():
m_rhs(NULL),
m_lhs(NULL),
m_cached_calculate(NULL)
/*
pre:
effect: constucts an empty COperator2Expr
*/
{
}
COperator2Expr::~COperator2Expr()
/*
pre:
effect: deletes the object
*/
{
if (m_lhs)
m_lhs->Release();
if (m_rhs)
m_rhs->Release();
if (m_cached_calculate)
m_cached_calculate->Release();
}
CValue* COperator2Expr::Calculate()
/*
pre:
ret: a new object containing the result of applying operator m_op to m_lhs
and m_rhs
*/
{
bool leftmodified,rightmodified;
leftmodified = m_lhs->NeedsRecalculated();
rightmodified = m_rhs->NeedsRecalculated();
// if no modifications on both left and right subtree, and result is already calculated
// then just return cached result...
if (!leftmodified && !rightmodified && (m_cached_calculate))
{
// not modified, just return m_cached_calculate
} else {
// if not yet calculated, or modified...
if (m_cached_calculate) {
m_cached_calculate->Release();
m_cached_calculate=NULL;
}
CValue* ffleft = m_lhs->Calculate();
CValue* ffright = m_rhs->Calculate();
ffleft->SetOwnerExpression(this);//->m_pOwnerExpression=this;
ffright->SetOwnerExpression(this);//->m_pOwnerExpression=this;
m_cached_calculate = ffleft->Calc(m_op,ffright);
//if (m_cached_calculate)
// m_cached_calculate->Action(CValue::SETOWNEREXPR,&CVoidValue(this,false,CValue::STACKVALUE));
ffleft->Release();
ffright->Release();
}
return m_cached_calculate->AddRef();
}
/*
bool COperator2Expr::IsInside(float x, float y, float z,bool bBorderInclude)
{
bool inside;
inside = false;
switch (m_op)
{
case VALUE_ADD_OPERATOR: {
// inside = first || second; // optimized with early out if first is inside
// todo: calculate smallest leaf first ! is much faster...
bool second;//first ;//,second;
//first = m_lhs->IsInside(x,y,z) ;
second = m_rhs->IsInside(x,y,z,bBorderInclude) ;
if (second)
return true; //early out
// second = m_rhs->IsInside(x,y,z) ;
return m_lhs->IsInside(x,y,z,bBorderInclude) ;
break;
}
case VALUE_SUB_OPERATOR: {
//inside = first && !second; // optimized with early out
// todo: same as with add_operator: calc smallest leaf first
bool second;//first ;//,second;
//first = m_lhs->IsInside(x,y,z) ;
second = m_rhs->IsInside(x,y,z,bBorderInclude);
if (second)
return false;
// second space get subtracted -> negate!
//second = m_rhs->IsInside(x,y,z);
return (m_lhs->IsInside(x,y,z,bBorderInclude));
break;
}
default:{
assert(false);
// not yet implemented, only add or sub csg operations
}
}
return inside;
}
bool COperator2Expr::IsRightInside(float x, float y, float z,bool bBorderInclude) {
return m_rhs->IsInside(x,y,z,bBorderInclude) ;
}
bool COperator2Expr::IsLeftInside(float x, float y, float z,bool bBorderInclude) {
return m_lhs->IsInside(x,y,z,bBorderInclude);
}
*/
bool COperator2Expr::NeedsRecalculated() {
// added some lines, just for debugging purposes, it could be a one-liner :)
//bool modleft
//bool modright;
assertd(m_lhs);
assertd(m_rhs);
//modright = m_rhs->NeedsRecalculated();
if (m_rhs->NeedsRecalculated()) // early out
return true;
return m_lhs->NeedsRecalculated();
//modleft = m_lhs->NeedsRecalculated();
//return (modleft || modright);
}
CExpression* COperator2Expr::CheckLink(std::vector<CBrokenLinkInfo*>& brokenlinks) {
// if both children are 'dead', return NULL
// if only one child is alive, return that child
// if both childresn are alive, return this
// bool leftalive = true,rightalive=true;
/* Does this mean the function will always bomb? */
assertd(false);
assert(m_lhs);
assert(m_rhs);
/*
if (m_cached_calculate)
m_cached_calculate->Action(CValue::REFRESH_CACHE);
CExpression* newlhs = m_lhs->CheckLink(brokenlinks);
CExpression* newrhs = m_rhs->CheckLink(brokenlinks);
if (m_lhs != newlhs)
{
brokenlinks.push_back(new CBrokenLinkInfo(&m_lhs,m_lhs));
}
if (m_rhs != newrhs)
{
brokenlinks.push_back(new CBrokenLinkInfo(&m_rhs,m_rhs));
}
m_lhs = newlhs;
m_rhs = newrhs;
if (m_lhs && m_rhs) {
return this;
}
AddRef();
if (m_lhs)
return Release(m_lhs->AddRef());
if (m_rhs)
return Release(m_rhs->AddRef());
/
*/
return Release();
}
bool COperator2Expr::MergeExpression(CExpression *otherexpr)
{
if (m_lhs)
{
if (m_lhs->GetExpressionID() == CExpression::CCONSTEXPRESSIONID)
{
// cross fingers ;) replace constexpr by new tree...
m_lhs->Release();
m_lhs = otherexpr->AddRef();
return true;
}
}
assertd(false);
return false;
}
void COperator2Expr::BroadcastOperators(VALUE_OPERATOR op)
{
if (m_lhs)
m_lhs->BroadcastOperators(m_op);
if (m_rhs)
m_rhs->BroadcastOperators(m_op);
}
|