Files
EgtGeomKernel/SurfBezier.cpp
T
Daniele Bariletti c303b1273d EgtGeomKernel :
- aggiunta la funzione per creare una sfera come superficie bezier
- aggiunta la funzione per tagliare una superificie bezier con un piano.
2024-02-20 12:28:45 +01:00

1912 lines
67 KiB
C++

//----------------------------------------------------------------------------
// EgalTech 2020-2020
//----------------------------------------------------------------------------
// File : SurfBezier.cpp Data : 11.08.20 Versione : 2.2h2
// Contenuto : Implementazione della classe Superfici Bezier.
//
//
//
// Modifiche : 22.03.20 DS Creazione modulo.
// 11.08.20 DS Trasformata in MultiPatch.
//
//----------------------------------------------------------------------------
//--------------------------- Include ----------------------------------------
#include "stdafx.h"
#include "SurfBezier.h"
#include "GeoObjFactory.h"
#include "NgeWriter.h"
#include "NgeReader.h"
#include "Bernstein.h"
#include "CurveBezier.h"
#include "CurveComposite.h"
#include "Tree.h"
#include "Triangulate.h"
#include "SurfTriMesh.h"
#include "/EgtDev/Include/EGkSfrCreate.h"
#include "/EgtDev/Include/EGkStmFromTriangleSoup.h"
#include "/EgtDev/Include/EGkStringUtils3d.h"
#include "/EgtDev/Include/EGkUiUnits.h"
#include "/EgtDev/Include/EgtNumUtils.h"
#include "/EgtDev/Include/EgtPointerOwner.h"
#include "/EgtDev/Include/EGkIntersPlaneSurfTm.h"
#include "/EgtDev/Include/EGkChainCurves.h"
#include "/EgtDev/Include/EGkIntersLineSurfBez.h"
#include "/EgtDev/Include/EGkDistPointSurfTm.h"
#include "/EgtDev/Extern/Eigen/Dense"
#include "/EgtDev/Include/EGkCurveComposite.h"
#include "/EgtDev/Include/EGkGeoObjSave.h"
using namespace std ;
//----------------------------------------------------------------------------
GEOOBJ_REGISTER( SRF_BEZIER, NGE_S_BEZ, SurfBezier) ;
//----------------------------------------------------------------------------
SurfBezier::SurfBezier( void)
: m_pSTM( nullptr), m_nStatus( TO_VERIFY), m_nDegU(), m_nDegV(), m_nSpanU(), m_nSpanV(), m_bRat( false),
m_bTrimmed( false), m_pTrimReg( nullptr), m_nTempProp{0,0}, m_dTempParam{0.0,0.0}
{
}
//----------------------------------------------------------------------------
SurfBezier::~SurfBezier( void)
{
ResetTrimRegion() ;
m_OGrMgr.Reset() ;
ResetAuxSurf() ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::Init( int nDegU, int nDegV, int nSpanU, int nSpanV, bool bIsRational)
{
// verifico validit grado
if ( nDegU < 1 || nDegU > MAXDEG || nDegV < 1 || nDegV > MAXDEG || nSpanU < 1 || nSpanV < 1)
return false ;
// imposto gradi e flag di razionale
m_nDegU = nDegU ;
m_nDegV = nDegV ;
m_nSpanU = nSpanU ;
m_nSpanV = nSpanV ;
m_bRat = bIsRational ;
m_bTrimmed = false ;
ResetTrimRegion() ;
// dimensiono i vettori dei punti e dei pesi
m_vPtCtrl.assign( GetDim(), ORIG) ;
if ( bIsRational)
m_vWeCtrl.assign( GetDim(), 1) ;
else
m_vWeCtrl.clear() ;
m_nStatus = TO_VERIFY ;
// imposto ricalcolo della grafica
ResetAuxSurf() ;
m_OGrMgr.Reset() ;
return Validate() ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::SetControlPoint( int nInd, const Point3d& ptCtrl)
{
// verifico validit indice
if ( m_nStatus != OK || m_bRat || nInd < 0 || nInd >= GetDim())
return false ;
// assegno il valore
m_vPtCtrl[nInd] = ptCtrl ;
// se razionale, metto il peso a 1
if ( m_bRat)
m_vWeCtrl[nInd] = 1 ;
// imposto ricalcolo della grafica
ResetAuxSurf() ;
m_OGrMgr.Reset() ;
return true ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::SetControlPoint( int nInd, const Point3d& ptCtrl, double dW)
{
// verifico validit, razionalit e indice
if ( m_nStatus != OK || ! m_bRat || nInd < 0 || nInd >= GetDim())
return false ;
// verifico che il peso non sia nullo o negativo
if ( dW < EPS_SMALL)
return false ;
// assegno il valore e il peso
m_vPtCtrl[nInd] = ptCtrl ;
m_vWeCtrl[nInd] = dW ;
// imposto ricalcolo della grafica
ResetAuxSurf() ;
m_OGrMgr.Reset() ;
return true ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::SetTrimRegion( ISurfFlatRegion& sfrTrimReg, bool bIntersectOrSubtract)
{
// verifico la regione passata
if ( &sfrTrimReg == nullptr || ! sfrTrimReg.IsValid())
return false ;
// se la normale ha z negativa ribalto la superficie, sennò le operazioni di intersect e subtract non funzionano
if ( sfrTrimReg.GetNormVersor().z < 0)
sfrTrimReg.Invert() ;
// limito la regione allo spazio parametrico della superficie
PtrOwner< ISurfFlatRegion> pSfrTrim( GetSurfFlatRegionRectangle( SBZ_TREG_COEFF * m_nSpanU, SBZ_TREG_COEFF * m_nSpanV)) ;
// bIntersectOrSubtract == true per ottenere lo spazio parametrico trimmato devo fare l'INTERSEZIONE tra il rettangolo totale e l'area passata
if ( bIntersectOrSubtract) {
if ( IsNull( pSfrTrim) || ! pSfrTrim->Intersect( sfrTrimReg) || ! pSfrTrim->IsValid())
return false ;
}
// bIntersectOrSubtract == false per ottenere lo spazio parametrico trimmato devo fare la SOTTRAZIONE tra il rettangolo totale e l'area passata
else {
if ( IsNull( pSfrTrim) || ! pSfrTrim->Subtract( sfrTrimReg) || ! pSfrTrim->IsValid())
return false ;
}
ResetAuxSurf() ;
delete m_pSTM ;
// assegno la regione di trim
m_bTrimmed = true ;
delete m_pTrimReg ;
m_pTrimReg = GetBasicSurfFlatRegion( Release( pSfrTrim)) ;
return true ;
}
//----------------------------------------------------------------------------
SurfFlatRegion*
SurfBezier::GetTrimRegion( void) const
{
if ( ! m_bTrimmed || m_pTrimReg == nullptr )
return nullptr ;
return m_pTrimReg ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::GetInfo( int& nDegU, int& nDegV, int& nSpanU, int& nSpanV, bool& bIsRat, bool& bTrimmed) const
{
// verifico validit superficie
if ( m_nStatus != OK)
return false ;
// restituisco gradi e flag di razionale
nDegU = m_nDegU ;
nDegV = m_nDegV ;
nSpanU = m_nSpanU ;
nSpanV = m_nSpanV ;
bIsRat = m_bRat ;
bTrimmed = m_bTrimmed ;
return true ;
}
//----------------------------------------------------------------------------
const Point3d&
SurfBezier::GetControlPoint( int nInd, bool* pbOk) const
{
// verifico validit e indice
if ( m_nStatus != OK || nInd < 0 || nInd >= GetDim()) {
if ( pbOk != NULL)
*pbOk = false ;
return ORIG ;
}
// ritorno i dati
if ( pbOk != NULL)
*pbOk = true ;
return m_vPtCtrl[nInd] ;
}
//----------------------------------------------------------------------------
double
SurfBezier::GetControlWeight( int nInd, bool* pbOk) const
{
// verifico validit, razionalit e indice
if ( m_nStatus != OK || ! m_bRat || nInd < 0 || nInd >= GetDim()) {
if ( pbOk != NULL)
*pbOk = false ;
return 0 ;
}
// ritorno i dati
if ( pbOk != NULL)
*pbOk = true ;
return m_vWeCtrl[nInd] ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::IsAPoint( void) const
{
// verifico lo stato
if ( m_nStatus != OK)
return false ;
// ciclo sui punti
for ( int i = 1 ; i < GetDim() ; ++ i) {
if ( ! AreSamePointApprox( m_vPtCtrl[0], m_vPtCtrl[i]))
return false ;
}
return true ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::GetArea( double& dArea) const
{
// controllo parametro di ritorno
if ( &dArea == nullptr)
return false ;
// inizio con area nulla
dArea = 0 ;
// calcolo l'area
if ( m_pSTM == nullptr)
if ( ! GetAuxSurf())
return false ;
return m_pSTM->GetArea( dArea) ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::GetCentroid( Point3d& ptCen) const
{
// controllo parametro di ritorno
if ( &ptCen == nullptr)
return false ;
// inizio con centro nell'origine
ptCen = ORIG ;
// calcolo il baricentro
if ( m_pSTM == nullptr)
if ( ! GetAuxSurf())
return false ;
return m_pSTM->GetCentroid( ptCen) ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::GetPointD1D2( double dU, double dV, Side nUs, Side nVs,
Point3d& ptPos,
Vector3d* pvtDerU, Vector3d* pvtDerV, Vector3d* pvtDerUU, Vector3d* pvtDerVV, Vector3d* pvtDerUV) const
{
// la curva deve essere validata
if ( m_nStatus != OK)
return false ;
// i parametri U e V devono essere compresi tra 0 e il corrispondente numero di Span
dU = Clamp( dU, 0., double( m_nSpanU)) ;
dV = Clamp( dV, 0., double( m_nSpanV)) ;
// determino gli intervalli di span e riduco i parametri in essi
int nBsU = min( int( dU), m_nSpanU - 1) ;
double dLocU = dU - nBsU ;
if ( abs( dLocU) < 5 * EPS_PARAM && nBsU > 0 && nUs == ISurfBezier::FROM_MINUS) {
-- nBsU ;
dLocU = 1 ;
}
else if ( abs( dLocU) > 1 - 5 * EPS_PARAM && nBsU < m_nSpanU - 1 && nUs == ISurfBezier::FROM_PLUS) {
++ nBsU ;
dLocU = 0 ;
}
int nOffsU = nBsU * m_nDegU ;
int nBsV = min( int( dV), m_nSpanV - 1) ;
double dLocV = dV - nBsV ;
if ( abs( dLocV) < 5 * EPS_PARAM && nBsV > 0 && nVs == ISurfBezier::FROM_MINUS) {
-- nBsV ;
dLocV = 1 ;
}
else if ( abs( dLocV) > 1 - 5 * EPS_PARAM && nBsV < m_nSpanV - 1 && nVs == ISurfBezier::FROM_PLUS) {
++ nBsV ;
dLocV = 0 ;
}
int nOffsV = nBsV * m_nDegV ;
// se forma polinomiale (o integrale)
if ( ! m_bRat) {
// calcolo dei polinomi di Bernstein per U di grado opportuno
DBLVECTOR vBernU( m_nDegU + 1) ;
GetAllBernstein( dLocU, m_nDegU - 2, vBernU) ;
//// se richiesto, calcolo della derivata seconda
// if ( pvtDer2 != nullptr && pvtDer1 != nullptr) {
// *pvtDer2 = V_NULL ;
// for ( int i = 0 ; i <= m_nDeg - 2 ; ++ i) {
// *pvtDer2 += vBern[i] * ( m_vPtCtrl[i+2] + m_vPtCtrl[i] - 2 * m_vPtCtrl[i+1]) ;
// }
// *pvtDer2 *= m_nDeg * ( m_nDeg - 1) ;
// }
// aumento il grado
IncreaseAllBernsteinOneDegree( dLocU, m_nDegU - 1, vBernU) ;
// se richiesto, calcolo dei vettori intermedi per la derivata prima rispetto ad U
VCT3DVECTOR vtTemp1( m_nDegV + 1, V_NULL) ;
if ( pvtDerU != nullptr) {
for ( int j = 0 ; j <= m_nDegV ; ++ j) {
for ( int i = 0 ; i <= m_nDegU - 1 ; ++ i)
vtTemp1[j] += vBernU[i] * ( m_vPtCtrl[GetInd( nOffsU + i + 1, nOffsV + j)] - m_vPtCtrl[GetInd( nOffsU + i, nOffsV + j)]) ;
vtTemp1[j] *= m_nDegU ;
}
}
// aumento il grado
IncreaseAllBernsteinOneDegree( dLocU, m_nDegU, vBernU) ;
// calcolo dei punti intermedi
PNTVECTOR ptTemp( m_nDegV + 1, ORIG) ;
for ( int j = 0 ; j <= m_nDegV ; ++ j) {
for ( int i = 0 ; i <= m_nDegU ; ++ i)
ptTemp[j] += vBernU[i] * m_vPtCtrl[GetInd( nOffsU + i, nOffsV + j)] ;
}
// calcolo dei polinomi di Bernstein per V di grado opportuno
DBLVECTOR vBernV( m_nDegV + 1) ;
GetAllBernstein( dLocV, m_nDegV - 1, vBernV) ;
// se richiesto, calcolo della derivata prima rispetto a V
if ( pvtDerV != nullptr) {
*pvtDerV = V_NULL ;
for ( int j = 0 ; j <= m_nDegV - 1 ; ++ j)
*pvtDerV += vBernV[j] * ( ptTemp[j+1] - ptTemp[j]) ;
*pvtDerV *= m_nDegV ;
}
// aumento il grado
IncreaseAllBernsteinOneDegree( dLocV, m_nDegV, vBernV) ;
// calcolo del punto
ptPos = ORIG ;
for ( int j = 0 ; j <= m_nDegV ; ++ j)
ptPos += vBernV[j] * ptTemp[j] ;
// se richiesto, calcolo della derivata prima rispetto a U
if ( pvtDerU != nullptr) {
*pvtDerU = V_NULL ;
for ( int j = 0 ; j <= m_nDegV ; ++ j)
*pvtDerU += vBernV[j] * vtTemp1[j] ;
}
}
// altrimenti forma razionale
else {
// porto i punti in forma omogenea moltiplicandoli per i pesi
PNTVECTOR vPtWCtrl( GetLocDim()) ;
DBLVECTOR vWeCtrl( GetLocDim()) ;
for ( int j = 0 ; j <= m_nDegV ; ++ j) {
for ( int i = 0 ; i <= m_nDegU ; ++ i) {
vPtWCtrl[GetLocInd( i, j)] = m_vWeCtrl[GetInd( nOffsU + i, nOffsV + j)] * m_vPtCtrl[GetInd( nOffsU + i, nOffsV + j)] ;
vWeCtrl[GetLocInd( i, j)] = m_vWeCtrl[GetInd( nOffsU + i, nOffsV + j)] ;
}
}
// calcolo dei polinomi di Bernstein di grado opportuno
DBLVECTOR vBernU( m_nDegU + 1) ;
GetAllBernstein( dLocU, m_nDegU - 2, vBernU) ;
//// se richiesto, calcolo della derivata seconda
// double dW2 = 0 ;
// if ( pvtDer2 != nullptr && pvtDer1 != nullptr) {
// *pvtDer2 = V_NULL ;
// for ( int i = 0 ; i <= m_nDeg - 2 ; ++ i) {
// *pvtDer2 += vBern[i] * ( vPtWCtrl[i+2] + vPtWCtrl[i] - 2 * vPtWCtrl[i+1]) ;
// dW2 += vBern[i] * ( m_vWeCtrl[i+2] + m_vWeCtrl[i] - 2 * m_vWeCtrl[i+1]) ;
// }
// *pvtDer2 *= m_nDeg * ( m_nDeg - 1) ;
// dW2 *= m_nDeg * ( m_nDeg - 1) ;
// }
// aumento il grado
IncreaseAllBernsteinOneDegree( dLocU, m_nDegU - 1, vBernU) ;
// se richiesto, calcolo dei vettori intermedi per la derivata prima rispetto ad U
VCT3DVECTOR vtTemp1( m_nDegV + 1, V_NULL) ;
DBLVECTOR dTemp1( m_nDegV + 1, 0) ;
if ( pvtDerU != nullptr) {
for ( int j = 0 ; j <= m_nDegV ; ++ j) {
for ( int i = 0 ; i <= m_nDegU - 1 ; ++ i) {
vtTemp1[j] += vBernU[i] * ( vPtWCtrl[GetLocInd( i + 1, j)] - vPtWCtrl[GetLocInd( i, j)]) ;
dTemp1[j] += vBernU[i] * ( vWeCtrl[GetLocInd( i + 1, j)] - vWeCtrl[GetLocInd( i, j)]) ;
}
vtTemp1[j] *= m_nDegU ;
dTemp1[j] *= m_nDegU ;
}
}
// aumento il grado
IncreaseAllBernsteinOneDegree( dLocU, m_nDegU, vBernU) ;
// calcolo dei punti e pesi intermedi
PNTVECTOR ptTempW( m_nDegV + 1, ORIG) ;
DBLVECTOR dTempW( m_nDegV + 1, 0) ;
for ( int j = 0 ; j <= m_nDegV ; ++ j) {
for ( int i = 0 ; i <= m_nDegU ; ++ i) {
ptTempW[j] += vBernU[i] * vPtWCtrl[GetLocInd( i, j)] ;
dTempW[j] += vBernU[i] * vWeCtrl[GetLocInd( i, j)] ;
}
}
// calcolo dei polinomi di Bernstein per V di grado opportuno
DBLVECTOR vBernV( m_nDegV + 1) ;
GetAllBernstein( dLocV, m_nDegV - 1, vBernV) ;
// se richiesto, calcolo della derivata prima rispetto a V
double dW1v = 0 ;
if ( pvtDerV != nullptr) {
*pvtDerV = V_NULL ;
for ( int j = 0 ; j <= m_nDegV - 1 ; ++ j) {
*pvtDerV += vBernV[j] * ( ptTempW[j+1] - ptTempW[j]) ;
dW1v += vBernV[j] * ( dTempW[j+1] - dTempW[j]) ;
}
*pvtDerV *= m_nDegV ;
dW1v *= m_nDegV ;
}
// aumento il grado
IncreaseAllBernsteinOneDegree( dLocV, m_nDegV, vBernV) ;
// calcolo del punto
double dW = 0 ;
ptPos = ORIG ;
for ( int j = 0 ; j <= m_nDegV ; ++ j) {
ptPos += vBernV[j] * ptTempW[j] ;
dW += vBernV[j] * dTempW[j] ;
}
// ritrasformo da forma omogenea a forma standard
double dInvW = 1 / ( ( dW > EPS_ZERO) ? dW : EPS_ZERO) ;
ptPos *= dInvW ;
// se richiesto, calcolo della derivata prima rispetto a U
if ( pvtDerU != nullptr) {
*pvtDerU = V_NULL ;
double dW1u = 0 ;
for ( int j = 0 ; j <= m_nDegV ; ++ j) {
*pvtDerU += vBernV[j] * vtTemp1[j] ;
dW1u += vBernV[j] * dTemp1[j] ;
}
Vector3d vtPos( ptPos.x, ptPos.y, ptPos.z) ;
*pvtDerU = ( *pvtDerU - dW1u * vtPos) * dInvW ;
}
// se richiesto, completo il calcolo della derivata prima rispetto a V
if ( pvtDerV != nullptr) {
Vector3d vtPos( ptPos.x, ptPos.y, ptPos.z) ;
*pvtDerV = ( *pvtDerV - dW1v * vtPos) * dInvW ;
}
//if ( pvtDer1 != nullptr) {
// Vector3d vtPos( ptPos.x, ptPos.y, ptPos.z) ;
// *pvtDer1 = ( *pvtDer1 - dW1 * vtPos) * dInvW ;
// if ( pvtDer2 != nullptr)
// *pvtDer2 = ( *pvtDer2 - 2 * dW1 * *pvtDer1 - dW2 * vtPos) * dInvW ;
//}
}
return true ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::GetPointNrmD1D2( double dU, double dV, Side nUs, Side nVs,
Point3d& ptPos, Vector3d& vtN,
Vector3d* pvtDerU, Vector3d* pvtDerV, Vector3d* pvtDerUU, Vector3d* pvtDerVV, Vector3d* pvtDerUV) const
{
// la superficie deve essere validata
if ( m_nStatus != OK)
return false ;
// i parametri U e V devono essere compresi tra 0 e il corrispondente numero di Span
dU = Clamp( dU, 0., double( m_nSpanU)) ;
dV = Clamp( dV, 0., double( m_nSpanV)) ;
// eseguo calcolo del punto con le derivate prime
Vector3d vtDerU ;
if ( pvtDerU == nullptr)
pvtDerU = &vtDerU ;
Vector3d vtDerV ;
if ( pvtDerV == nullptr)
pvtDerV = &vtDerV ;
GetPointD1D2( dU, dV, nUs, nVs, ptPos, pvtDerU, pvtDerV) ;
// calcolo la normale e la verifico
vtN = *pvtDerU ^ *pvtDerV ;
if ( vtN.Normalize())
return true ;
// se solo una delle due derivate piccola, mi sposto lungo il relativo parametro e uso le tangenti
if ( pvtDerU->Len() < EPS_SMALL && pvtDerV->Len() > 10 * EPS_SMALL) {
double dCoeff = ( dU - 1000 * EPS_PARAM < 0. ? 1 : -1) ;
double dUm = dU + 1000 * EPS_PARAM * dCoeff ;
Point3d ptTmp ;
Vector3d vtTmpU, vtTmpV ;
GetPointD1D2( dUm, dV, nUs, nVs, ptTmp, &vtTmpU, &vtTmpV) ;
vtN = ( *pvtDerV ^ vtTmpV) * dCoeff ;
if ( vtN.Normalize())
return true ;
}
if ( pvtDerU->Len() > 10 * EPS_SMALL && pvtDerV->Len() < EPS_SMALL) {
double dCoeff = ( dV - 1000 * EPS_PARAM < 0. ? 1 : -1) ;
double dVm = dV + 1000 * EPS_PARAM * dCoeff ;
Point3d ptTmp ;
Vector3d vtTmpU, vtTmpV ;
GetPointD1D2( dU, dVm, nUs, nVs, ptTmp, &vtTmpU, &vtTmpV) ;
vtN = ( vtTmpU ^ *pvtDerU) * dCoeff ;
if ( vtN.Normalize())
return true ;
}
// ricalcolo con una piccola variazione di entrambi i parametri
double dUm ;
if ( dU - 100 * EPS_PARAM < 0.)
dUm = dU + 100 * EPS_PARAM ;
else
dUm = dU - 100 * EPS_PARAM ;
double dVm ;
if ( dV - 100 * EPS_PARAM < 0.)
dVm = dV + 100 * EPS_PARAM ;
else
dVm = dV - 100 * EPS_PARAM ;
Point3d ptTmp ;
GetPointD1D2( dUm, dVm, nUs, nVs, ptTmp, pvtDerU, pvtDerV) ;
vtN = *pvtDerU ^ *pvtDerV ;
return vtN.Normalize() ;
}
//----------------------------------------------------------------------------
SurfBezier*
SurfBezier::Clone( void) const
{
// alloco oggetto
SurfBezier* pSbz = new( nothrow) SurfBezier ;
if ( pSbz != nullptr) {
if ( ! pSbz->CopyFrom( *this)) {
delete pSbz ;
return nullptr ;
}
}
return pSbz ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::CopyFrom( const IGeoObj* pGObjSrc)
{
const SurfBezier* pSbz = GetBasicSurfBezier( pGObjSrc) ;
if ( pSbz == nullptr)
return false ;
return CopyFrom( *pSbz) ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::CopyFrom( const SurfBezier& sbSrc)
{
if ( &sbSrc == this)
return true ;
if ( ! Init( sbSrc.m_nDegU, sbSrc.m_nDegV, sbSrc.m_nSpanU, sbSrc.m_nSpanV, sbSrc.m_bRat))
return false ;
m_nStatus = sbSrc.m_nStatus ;
m_vPtCtrl = sbSrc.m_vPtCtrl ;
if ( sbSrc.m_bRat)
m_vWeCtrl = sbSrc.m_vWeCtrl ;
if ( sbSrc.m_bTrimmed) {
m_bTrimmed = true ;
m_pTrimReg = sbSrc.m_pTrimReg->Clone() ;
}
m_nTempProp[0] = sbSrc.m_nTempProp[0] ;
m_nTempProp[1] = sbSrc.m_nTempProp[1] ;
m_dTempParam[0] = sbSrc.m_dTempParam[0] ;
m_dTempParam[1] = sbSrc.m_dTempParam[1] ;
return true ;
}
//----------------------------------------------------------------------------
GeoObjType
SurfBezier::GetType( void) const
{
return static_cast<GeoObjType>( GEOOBJ_GETTYPE( SurfBezier)) ;
}
//----------------------------------------------------------------------------
const string&
SurfBezier::GetTitle( void) const
{
static const string sTitle = "SurfBezier" ;
return sTitle ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::Dump( string& sOut, bool bMM, const char* szNewLine) const
{
// verifico validit superficie
if ( m_nStatus != OK)
sOut += string( "Status=Invalid") + szNewLine ;
// area
double dArea ;
GetArea( dArea) ;
sOut += "Area=" + ToString( GetAreaInUiUnits( dArea, bMM),1) + szNewLine ;
// parametri : flag razionale
sOut += ( m_bRat ? "Rat" : "Int") ;
// flag trimmata
sOut += ( m_bTrimmed ? " Trim " : " Full") ;
// gradi in U e V
sOut += " DegU=" + ToString( m_nDegU) + " DegV=" + ToString( m_nDegV) ;
// pezze in U e V
sOut += " SpanU=" + ToString( m_nSpanU) + " SpanV=" + ToString( m_nSpanV) + szNewLine ;
// ciclo sui punti di controllo ( con pesi se razionale)
for ( int i = 0 ; i < GetDim() ; ++ i) {
sOut += "PC(" + ToString( GetInUiUnits( m_vPtCtrl[i], bMM), 3) ;
if ( m_bRat)
sOut += "," + ToString( m_vWeCtrl[i], 3) ;
sOut += string( ")") + szNewLine ;
}
return true ;
}
//----------------------------------------------------------------------------
int
SurfBezier::GetNgeId( void) const
{
return GEOOBJ_GETNGEID( SurfBezier) ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::Save( NgeWriter& ngeOut) const
{
// flag razionale
if ( ! ngeOut.WriteBool( m_bRat, ";"))
return false ;
// flag trimmata
if ( ! ngeOut.WriteBool( m_bTrimmed, ";"))
return false ;
// gradi
if ( ! ngeOut.WriteInt( m_nDegU, ",") ||
! ngeOut.WriteInt( m_nDegV, ";", false))
return false ;
// pezze
if ( ! ngeOut.WriteInt( m_nSpanU, ",") ||
! ngeOut.WriteInt( m_nSpanV, ";", true))
return false ;
// ciclo sui punti di controllo ( con pesi se razionale)
for ( int i = 0 ; i < GetDim() ; ++ i) {
if ( ! m_bRat) {
if ( ! ngeOut.WritePoint( m_vPtCtrl[i], ";", true))
return false ;
}
else {
if ( ! ngeOut.WritePointW( m_vPtCtrl[i], m_vWeCtrl[i], ";", true))
return false ;
}
}
// se trimmata, scrittura della regione
if ( m_bTrimmed) {
// recupero il gestore di lettura/scrittura della regione
const IGeoObjRW* pSFrRW = dynamic_cast<const IGeoObjRW*>( m_pTrimReg) ;
if ( pSFrRW == nullptr)
return false ;
// salvataggio della regione
if ( ! pSFrRW->Save( ngeOut))
return false ;
}
return true ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::Load( NgeReader& ngeIn)
{
// imposto ricalcolo della grafica
ResetAuxSurf() ;
m_OGrMgr.Reset() ;
// leggo la prossima linea ( 3 parametri)
// recupero il flag razionale
bool bIsRat ;
if ( ! ngeIn.ReadBool( bIsRat, ";"))
return false ;
// recupero il flag trimmata
bool bTrimmed ;
if ( ! ngeIn.ReadBool( bTrimmed, ";"))
return false ;
// recupero i gradi
int nDegU, nDegV ;
if ( ! ngeIn.ReadInt( nDegU, ",") ||
! ngeIn.ReadInt( nDegV, ";"))
return false ;
// recupero le pezze
int nSpanU, nSpanV ;
if ( ! ngeIn.ReadInt( nSpanU, ",") ||
! ngeIn.ReadInt( nSpanV, ";", true))
return false ;
// inizializzo la superficie di Bezier
if ( ! Init( nDegU, nDegV, nSpanU, nSpanV, bIsRat))
return false ;
// se integrale
if ( ! bIsRat) {
// recupero e setto punti di controllo
Point3d ptP ;
for ( int i = 0 ; i < GetDim() ; ++ i) {
// leggo la prossima linea ( un punto)
if ( ! ngeIn.ReadPoint( ptP, ";", true))
return false ;
// lo assegno
if ( ! SetControlPoint( i, ptP))
return false ;
}
}
// altrimenti razionale
else {
// recupero e setto punti di controllo
Point3d ptP ;
double dW ;
for ( int i = 0 ; i < GetDim() ; ++ i) {
// leggo la prossima linea ( un punto con peso)
if ( ! ngeIn.ReadPointW( ptP, dW, ";", true))
return false ;
// lo assegno
if ( ! SetControlPoint( i, ptP, dW))
return false ;
}
}
// se trimmata, lettura della regione
if ( bTrimmed) {
m_bTrimmed = true ;
// creo l'oggetto
ResetTrimRegion() ;
m_pTrimReg = CreateBasicSurfFlatRegion() ;
if ( m_pTrimReg == nullptr)
return false ;
// ne leggo i dati
IGeoObjRW* pGObjRW = dynamic_cast<IGeoObjRW*>( m_pTrimReg) ;
if ( pGObjRW == nullptr || ! pGObjRW->Load( ngeIn))
return false ;
}
// eseguo validazione
return Validate() ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::Validate( void)
{
if ( m_nStatus == TO_VERIFY)
m_nStatus = ( ( m_nDegU * m_nDegV > 0 && m_vPtCtrl.size() > 0) ? OK : ERR) ;
return ( m_nStatus == OK) ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::GetLocalBBox( BBox3d& b3Loc, int nFlag) const
{
// basta approssimato
if ( ( nFlag & BBF_EXACT) == 0) {
for ( int i = 0 ; i < GetDim() ; ++ i) {
Point3d ptTemp = m_vPtCtrl[i] ;
b3Loc.Add( ptTemp) ;
}
return true ;
}
// deve essere preciso
else {
// verifico esistenza trimesh associata
if ( m_pSTM == nullptr)
if ( ! GetAuxSurf())
return false ;
// calcolo il box della trimesh
return m_pSTM->GetLocalBBox( b3Loc, nFlag) ;
}
}
//----------------------------------------------------------------------------
bool
SurfBezier::GetBBox( const Frame3d& frRef, BBox3d& b3Ref, int nFlag) const
{
// basta approssimato
if ( ( nFlag & BBF_EXACT) == 0) {
for ( int i = 0 ; i < GetDim() ; ++ i) {
Point3d ptTemp = m_vPtCtrl[i] ;
ptTemp.ToGlob( frRef) ;
b3Ref.Add( ptTemp) ;
}
return true ;
}
// deve essere preciso
else {
// verifico esistenza trimesh associata
if ( m_pSTM == nullptr)
if ( ! GetAuxSurf())
return false ;
// calcolo il box della trimesh
return m_pSTM->GetBBox( frRef, b3Ref, nFlag) ;
}
}
//----------------------------------------------------------------------------
bool
SurfBezier::Translate( const Vector3d& vtMove)
{
// la superficie deve essere validata
if ( m_nStatus != OK)
return false ;
// imposto ricalcolo della grafica
ResetAuxSurf() ;
m_OGrMgr.Reset() ;
// traslo i punti di controllo
for ( auto& ptP : m_vPtCtrl)
ptP.Translate( vtMove) ;
return true ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::Rotate( const Point3d& ptAx, const Vector3d& vtAx, double dCosAng, double dSinAng)
{
// la superficie deve essere validata
if ( m_nStatus != OK)
return false ;
// verifico validit dell'asse di rotazione
if ( vtAx.IsSmall())
return false ;
// imposto ricalcolo della grafica
ResetAuxSurf() ;
m_OGrMgr.Reset() ;
// ruoto i punti di controllo
for ( auto& ptP : m_vPtCtrl)
ptP.Rotate( ptAx, vtAx, dCosAng, dSinAng) ;
return true ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::Scale( const Frame3d& frRef, double dCoeffX, double dCoeffY, double dCoeffZ)
{
// la superficie deve essere validata
if ( m_nStatus != OK)
return false ;
// verifico non sia nulla
if ( abs( dCoeffX) < EPS_ZERO && abs( dCoeffY) < EPS_ZERO && abs( dCoeffZ) < EPS_ZERO)
return false ;
// calcolo bbox allineato con riferimento di scalatura e senza tener conto dello spessore
// lo scalo per verificare se tutto si riduce a un punto o una linea (solo se diretta come assi ref)
BBox3d b3Ref ;
if ( ! GetBBox( frRef, b3Ref))
return false ;
Vector3d vtDelta = b3Ref.GetMax() - b3Ref.GetMin() ;
bool bZeroX = ( abs( vtDelta.x * dCoeffX) < EPS_SMALL) ;
bool bZeroY = ( abs( vtDelta.y * dCoeffY) < EPS_SMALL) ;
bool bZeroZ = ( abs( vtDelta.z * dCoeffZ) < EPS_SMALL) ;
if ( ( bZeroX && bZeroY) || ( bZeroX && bZeroZ) || ( bZeroY && bZeroZ))
return false ;
// imposto ricalcolo della grafica
ResetAuxSurf() ;
m_OGrMgr.Reset() ;
// scalo i punti di controllo
for ( auto& ptP : m_vPtCtrl)
ptP.Scale( frRef, dCoeffX, dCoeffY, dCoeffZ) ;
return true ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::Mirror( const Point3d& ptOn, const Vector3d& vtNorm)
{
// la superficie deve essere validata
if ( m_nStatus != OK)
return false ;
// verifico validit del piano di specchiatura
if ( vtNorm.IsSmall())
return false ;
// imposto ricalcolo della grafica
ResetAuxSurf() ;
m_OGrMgr.Reset() ;
// specchio i punti di controllo
for ( auto& ptP : m_vPtCtrl)
ptP.Mirror( ptOn, vtNorm) ;
// eseguo invert per mantenere l'orientazione
return Invert() ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::Shear( const Point3d& ptOn, const Vector3d& vtNorm, const Vector3d& vtDir, double dCoeff)
{
// la superficie deve essere validata
if ( m_nStatus != OK)
return false ;
// verifico validit dei parametri
if ( vtNorm.IsSmall() || vtDir.IsSmall())
return false ;
// imposto ricalcolo della grafica
ResetAuxSurf() ;
m_OGrMgr.Reset() ;
// eseguo scorrimento dei punti di controllo
for ( auto& ptP : m_vPtCtrl)
ptP.Shear( ptOn, vtNorm, vtDir, dCoeff) ;
return true ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::ToGlob( const Frame3d& frRef)
{
// la superficie deve essere validata
if ( m_nStatus != OK)
return false ;
// verifico validit del frame
if ( frRef.GetType() == Frame3d::ERR)
return false ;
// se frame identit, non devo fare alcunch
if ( IsGlobFrame( frRef))
return true ;
// imposto ricalcolo della grafica
ResetAuxSurf() ;
m_OGrMgr.Reset() ;
// trasformo i punti di controllo
for ( auto& ptP : m_vPtCtrl)
ptP.ToGlob( frRef) ;
return true ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::ToLoc( const Frame3d& frRef)
{
// la superficie deve essere validata
if ( m_nStatus != OK)
return false ;
// verifico validit del frame
if ( frRef.GetType() == Frame3d::ERR)
return false ;
// se frame identit, non devo fare alcunch
if ( IsGlobFrame( frRef))
return true ;
// imposto ricalcolo della grafica
ResetAuxSurf() ;
m_OGrMgr.Reset() ;
// trasformo i punti di controllo
for ( auto& ptP : m_vPtCtrl)
ptP.ToLoc( frRef) ;
return true ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::LocToLoc( const Frame3d& frOri, const Frame3d& frDest)
{
// la superficie deve essere validata
if ( m_nStatus != OK)
return false ;
// verifico validit dei frame
if ( frOri.GetType() == Frame3d::ERR || frDest.GetType() == Frame3d::ERR)
return false ;
// se i due riferimenti coincidono, non devo fare alcunch
if ( AreSameFrame( frOri, frDest))
return true ;
// imposto ricalcolo della grafica
ResetAuxSurf() ;
m_OGrMgr.Reset() ;
// trasformo i punti di controllo
for ( auto& ptP : m_vPtCtrl)
ptP.LocToLoc( frOri, frDest) ;
return true ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::Invert( void)
{
// la superficie deve essere validata
if ( m_nStatus != OK)
return false ;
// imposto ricalcolo della grafica
ResetAuxSurf() ;
m_OGrMgr.Reset() ;
// inverto i punti del parametro U
PNTVECTOR vPtCtrl_inv( m_vPtCtrl.size()) ;
for ( int j = 0 ; j < m_nDegV * m_nSpanV + 1; ++j) {
for ( int i = 0 ; i < m_nDegU * m_nSpanU + 1; ++i) {
vPtCtrl_inv[ i + j * ( m_nDegU * m_nSpanU + 1)] = m_vPtCtrl[ ( m_nDegU * m_nSpanU - i) + j * ( m_nDegU * m_nSpanU + 1)] ;
}
}
m_vPtCtrl = vPtCtrl_inv ;
if ( m_bTrimmed) {
// inverto la flat region di trim
//( la specchio rispetto all'asse verticale)
Point3d pt( m_nSpanU * SBZ_TREG_COEFF/2, 0, 0) ;
Vector3d vt( 1, 0, 0) ;
m_pTrimReg->Mirror( pt, vt) ;
}
return true ;
}
//----------------------------------------------------------------------------
int
SurfBezier::GetSteps( int nDeg, int nSpan, double dLen, int nQuality) const
{
const double dCoeff[] = { 0, 1, 2, 2.4, 2.8, 3, 3, 3, 3, 3, 3, 3} ;
nDeg = Clamp( nDeg, 0, MAXDEG) ;
nSpan = max( nSpan, 1) ;
nQuality = Clamp( nQuality, 1, 10) ;
double dMult = sqrt( max( dLen / nSpan, 10.)) ;
return ( nSpan * int( dMult * dCoeff[nDeg] * 2 / nQuality)) ;
}
//----------------------------------------------------------------------------
CurveComposite*
SurfBezier::GetCurveOnU( double dV) const
{
// controlli
if ( dV < - EPS_PARAM || dV > m_nSpanV + EPS_PARAM)
return nullptr ;
dV = Clamp( dV, 0., double( m_nSpanV)) ;
// determino l'intervallo di span in V e riduco i parametri in essi
int nBsV = min( int( dV), m_nSpanV - 1) ;
double dLocV = dV - nBsV ;
int nOffsV = nBsV * m_nDegV ;
// se forma polinomiale (o integrale)
if ( ! m_bRat) {
// preparazione della curva composita
PtrOwner<CurveComposite> pCrvCo( CreateBasicCurveComposite()) ;
// ciclo sugli intervalli
for ( int k = 0 ; k < m_nSpanU ; ++ k) {
// preparazione della curva di Bezier
PtrOwner<CurveBezier> pCbz( CreateBasicCurveBezier()) ;
if ( IsNull( pCbz) || ! pCbz->Init( m_nDegU, false))
return nullptr ;
// calcolo dei polinomi di Bernstein per V di grado opportuno
DBLVECTOR vBernV( m_nDegV + 1) ;
GetAllBernstein( dLocV, m_nDegV, vBernV) ;
// calcolo offset in U
int nOffsU = k * m_nDegU ;
// calcolo dei punti di controllo della curva
for ( int i = 0 ; i <= m_nDegU ; ++ i) {
Point3d ptP = ORIG ;
for ( int j = 0 ; j <= m_nDegV ; ++ j)
ptP += vBernV[j] * m_vPtCtrl[GetInd( nOffsU + i, nOffsV + j)] ;
pCbz->SetControlPoint( i, ptP) ;
}
// inserisco la curva della pezza in quella complessiva
if ( ! IsNull( pCbz))
pCrvCo->AddCurve( Release( pCbz)) ;
}
return Release( pCrvCo) ;
}
// altrimenti forma razionale
else {
// preparazione della curva composita
PtrOwner<CurveComposite> pCrvCo( CreateBasicCurveComposite()) ;
// ciclo sugli intervalli
for ( int k = 0 ; k < m_nSpanU ; ++ k) {
// preparazione della curva di Bezier
PtrOwner<CurveBezier> pCbz( CreateBasicCurveBezier()) ;
if ( IsNull( pCbz) || ! pCbz->Init( m_nDegU, true))
return nullptr ;
// calcolo dei polinomi di Bernstein per V di grado opportuno
DBLVECTOR vBernV( m_nDegV + 1) ;
GetAllBernstein( dLocV, m_nDegV, vBernV) ;
// calcolo offset in U
int nOffsU = k * m_nDegU ;
// porto i punti in forma omogenea moltiplicandoli per i pesi
PNTVECTOR vPtWCtrl( GetLocDim()) ;
DBLVECTOR vWeCtrl( GetLocDim()) ;
for ( int j = 0 ; j <= m_nDegV ; ++ j) {
for ( int i = 0 ; i <= m_nDegU ; ++ i) {
vPtWCtrl[GetLocInd( i, j)] = m_vWeCtrl[GetInd( nOffsU + i, nOffsV + j)] * m_vPtCtrl[GetInd( nOffsU + i, nOffsV + j)] ;
vWeCtrl[GetLocInd( i, j)] = m_vWeCtrl[GetInd( nOffsU + i, nOffsV + j)] ;
}
}
// calcolo dei punti di controllo della curva
for ( int i = 0 ; i <= m_nDegU ; ++ i) {
Point3d ptP = ORIG ;
double dW = 0 ;
for ( int j = 0 ; j <= m_nDegV ; ++ j) {
ptP += vBernV[j] * vPtWCtrl[GetLocInd( i, j)] ;
dW += vBernV[j] * vWeCtrl[GetLocInd( i, j)] ;
}
double dInvW = 1 / ( ( dW > EPS_ZERO) ? dW : EPS_ZERO) ;
pCbz->SetControlPoint( i, ptP * dInvW, dW) ;
}
// inserisco la curva della pezza in quella complessiva
if ( ! IsNull( pCbz))
pCrvCo->AddCurve( Release( pCbz)) ;
}
return Release( pCrvCo) ;
}
}
//----------------------------------------------------------------------------
CurveComposite*
SurfBezier::GetCurveOnV( double dU) const
{
// controlli
if ( dU < - EPS_PARAM || dU > m_nSpanU + EPS_PARAM)
return nullptr ;
dU = Clamp( dU, 0., double( m_nSpanU)) ;
// determino l'intervallo di span in U e riduco i parametri in essi
int nBsU = min( int( dU), m_nSpanU - 1) ;
double dLocU = dU - nBsU ;
int nOffsU = nBsU * m_nDegU ;
// se forma polinomiale (o integrale)
if ( ! m_bRat) {
// preparazione della curva composita
PtrOwner<CurveComposite> pCrvCo( CreateBasicCurveComposite()) ;
// ciclo sugli intervalli
for ( int k = 0 ; k < m_nSpanV ; ++ k) {
// preparazione della curva di Bezier
PtrOwner<CurveBezier> pCbz( CreateBasicCurveBezier()) ;
if ( IsNull( pCbz) || ! pCbz->Init( m_nDegV, false))
return nullptr ;
// calcolo dei polinomi di Bernstein per U di grado opportuno
DBLVECTOR vBernU( m_nDegU + 1) ;
GetAllBernstein( dLocU, m_nDegU, vBernU) ;
// calcolo offset in V
int nOffsV = k * m_nDegV ;
// calcolo dei punti di controllo della curva
for ( int j = 0 ; j <= m_nDegV ; ++ j) {
Point3d ptP = ORIG ;
for ( int i = 0 ; i <= m_nDegU ; ++ i)
ptP += vBernU[i] * m_vPtCtrl[GetInd( nOffsU + i, nOffsV + j)] ;
pCbz->SetControlPoint( j, ptP) ;
}
// inserisco la curva della pezza in quella complessiva
if ( ! IsNull( pCbz))
pCrvCo->AddCurve( Release( pCbz)) ;
}
return Release( pCrvCo) ;
}
// altrimenti forma razionale
else {
// preparazione della curva composita
PtrOwner<CurveComposite> pCrvCo( CreateBasicCurveComposite()) ;
// ciclo sugli intervalli
for ( int k = 0 ; k < m_nSpanV ; ++ k) {
// preparazione della curva di Bezier
PtrOwner<CurveBezier> pCbz( CreateBasicCurveBezier()) ;
if ( IsNull( pCbz) || ! pCbz->Init( m_nDegV, true))
return nullptr ;
// calcolo dei polinomi di Bernstein per U di grado opportuno
DBLVECTOR vBernU( m_nDegU + 1) ;
GetAllBernstein( dLocU, m_nDegU, vBernU) ;
// calcolo offset in V
int nOffsV = k * m_nDegV ;
// porto i punti in forma omogenea moltiplicandoli per i pesi
PNTVECTOR vPtWCtrl( GetLocDim()) ;
DBLVECTOR vWeCtrl( GetLocDim()) ;
for ( int j = 0 ; j <= m_nDegV ; ++ j) {
for ( int i = 0 ; i <= m_nDegU ; ++ i) {
vPtWCtrl[GetLocInd( i, j)] = m_vWeCtrl[GetInd( nOffsU + i, nOffsV + j)] * m_vPtCtrl[GetInd( nOffsU + i, nOffsV + j)] ;
vWeCtrl[GetLocInd( i, j)] = m_vWeCtrl[GetInd( nOffsU + i, nOffsV + j)] ;
}
}
// calcolo dei punti di controllo della curva
for ( int j = 0 ; j <= m_nDegV ; ++ j) {
Point3d ptP = ORIG ;
double dW = 0 ;
for ( int i = 0 ; i <= m_nDegU ; ++ i) {
ptP += vBernU[i] * vPtWCtrl[GetLocInd( i, j)] ;
dW += vBernU[i] * vWeCtrl[GetLocInd( i, j)] ;
}
double dInvW = 1 / ( ( dW > EPS_ZERO) ? dW : EPS_ZERO) ;
pCbz->SetControlPoint( j, ptP * dInvW, dW) ;
}
// inserisco la curva della pezza in quella complessiva
if ( ! IsNull( pCbz))
pCrvCo->AddCurve( Release( pCbz)) ;
}
return Release( pCrvCo) ;
}
}
//----------------------------------------------------------------------------
CurveComposite*
SurfBezier::GetLoop( int nLoop) const
{
// Il primo loop sono le 4 isoparametriche di bordo concatenate
if ( ! m_bTrimmed ) {
if ( nLoop != 0 )
return nullptr ;
// Loop
PtrOwner<CurveComposite> pLoop( CreateBasicCurveComposite()) ;
// prima curva isoparametrica in U con V=0
PtrOwner<CurveComposite> pCrvCoU0( GetCurveOnU( 0)) ;
if ( ! IsNull( pCrvCoU0) && ! pCrvCoU0->IsAPoint())
pLoop->AddCurve( Release( pCrvCoU0)) ;
// seconda curva isoparametrica in V con U=m_nSpanU
PtrOwner<CurveComposite> pCrvCoV1( GetCurveOnV( m_nSpanU)) ;
if ( ! IsNull( pCrvCoV1) && ! pCrvCoV1->IsAPoint())
pLoop->AddCurve( Release( pCrvCoV1)) ;
// terza curva isoparametrica in U con V=m_nSpanV invertita
PtrOwner<CurveComposite> pCrvCoU1( GetCurveOnU( m_nSpanV)) ;
if ( ! IsNull( pCrvCoU1) && ! pCrvCoU1->IsAPoint()) {
pCrvCoU1->Invert() ;
pLoop->AddCurve( Release( pCrvCoU1)) ;
}
// quarta curva isoparametrica in V con U=0 invertita
PtrOwner<CurveComposite> pCrvCoV0( GetCurveOnV( 0)) ;
if ( ! IsNull( pCrvCoV0) && ! pCrvCoV0->IsAPoint()) {
pCrvCoV0->Invert() ;
pLoop->AddCurve( Release( pCrvCoV0)) ;
}
// se loop chiuso lo restituisco, altrimenti errore
return ( pLoop->IsClosed() ? Release( pLoop) : nullptr) ;
}
// la superficie trimmata, quindi devo cercare nei vari chunck il loop corrispondente
else {
if ( nLoop > m_pTrimReg->GetChunkCount())
return nullptr ;
else {
int nLoopCount = 0 ;
int nChunck = 0, nLoopLoc = 0;
INTVECTOR nLoopCountPerChunck ;
for ( int i = 0 ; i < m_pTrimReg->GetChunkCount() && nLoopCount != nLoop ; ++ i) {
int nLoopCountLoc = 0 ;
for ( int j = 0 ; j < m_pTrimReg->GetLoopCount( i) ; ++ j) {
++ nLoopCountLoc ;
++ nLoopCount ;
if ( nLoopCount != nLoop ) {
nChunck = i ;
nLoopLoc = j ;
break ;
}
}
nLoopCountPerChunck.push_back( nLoopCountLoc) ;
}
if ( nLoopCount < nLoop )
return nullptr ;
PtrOwner<CurveComposite> pLoop( GetBasicCurveComposite( m_pTrimReg->GetLoop( nChunck, nLoopLoc))) ;
return Release( pLoop) ;
}
}
}
//----------------------------------------------------------------------------
bool
SurfBezier::GetCurveOnU( double dV, int nStep, PolyLine& plCrvU) const
{
// controlli
plCrvU.Clear() ;
if ( dV < - EPS_PARAM || dV > m_nSpanV + EPS_PARAM)
return false ;
dV = Clamp( dV, 0., double( m_nSpanV)) ;
// recupero la curva
PtrOwner<CurveComposite> pCrvCo( GetCurveOnU( dV)) ;
if ( IsNull( pCrvCo))
return false ;
// ciclo sulle curve componenti (tutte curve di Bezier)
int i = 0 ;
const ICurve* pSCrv = pCrvCo->GetFirstCurve() ;
while ( pSCrv != nullptr) {
const CurveBezier* pCbz = GetBasicCurveBezier( pSCrv) ;
if ( pCbz == NULL)
return false ;
int nCurrSpanStep = nStep / m_nSpanU ;
if ( nCurrSpanStep <= 0) {
double dLenU = 0 ;
pCbz->GetApproxLength( dLenU) ;
nCurrSpanStep = GetSteps( m_nDegU, 1, dLenU, 2) ;
}
PolyLine plCurr ;
if ( ! pCbz->ApproxWithLines( nCurrSpanStep, plCurr))
return false ;
plCrvU.Join( plCurr, i) ;
++ i ;
pSCrv = pCrvCo->GetNextCurve() ;
}
return true ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::GetCurveOnV( double dU, int nStep, PolyLine& plCrvV) const
{
// controlli
plCrvV.Clear() ;
if ( dU < - EPS_PARAM || dU > m_nSpanU + EPS_PARAM)
return false ;
dU = Clamp( dU, 0., double( m_nSpanU)) ;
// recupero la curva
PtrOwner<CurveComposite> pCrvCo( GetCurveOnV( dU)) ;
if ( IsNull( pCrvCo))
return false ;
// ciclo sulle curve componenti (tutte curve di Bezier)
int i = 0 ;
const ICurve* pSCrv = pCrvCo->GetFirstCurve() ;
while ( pSCrv != nullptr) {
const CurveBezier* pCbz = GetBasicCurveBezier( pSCrv) ;
if ( pCbz == NULL)
return false ;
int nCurrSpanStep = nStep / m_nSpanU ;
if ( nCurrSpanStep <= 0) {
double dLenV = 0 ;
pCbz->GetApproxLength( dLenV) ;
nCurrSpanStep = GetSteps( m_nDegV, 1, dLenV, 2) ;
}
PolyLine plCurr ;
if ( ! pCbz->ApproxWithLines( nCurrSpanStep, plCurr))
return false ;
plCrvV.Join( plCurr, i) ;
++ i ;
pSCrv = pCrvCo->GetNextCurve() ;
}
return true ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::GetControlCurveOnU( int nIndV, PolyLine& plCtrlU) const
{
plCtrlU.Clear() ;
if ( nIndV < 0 || nIndV > m_nDegV * m_nSpanV)
return false ;
for ( int i = 0 ; i <= m_nDegU * m_nSpanU ; ++ i)
plCtrlU.AddUPoint( double( i) / m_nDegU, m_vPtCtrl[GetInd( i, nIndV)]) ;
return true ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::GetControlCurveOnV( int nIndU, PolyLine& plCtrlV) const
{
plCtrlV.Clear() ;
if ( nIndU < 0 || nIndU > m_nDegU * m_nSpanU)
return false ;
for ( int j = 0 ; j <= m_nDegV * m_nSpanV ; ++ j)
plCtrlV.AddUPoint( double( j) / m_nDegV, m_vPtCtrl[GetInd( nIndU, j)]) ;
return true ;
}
//----------------------------------------------------------------------------
double
SurfBezier::GetCurveOnUApproxLen( double dV) const
{
PtrOwner<CurveComposite> pCrvCo( GetCurveOnU( dV)) ;
double dLen ;
if ( IsNull( pCrvCo) || ! pCrvCo->GetApproxLength( dLen))
return 0 ;
return dLen ;
}
//----------------------------------------------------------------------------
double
SurfBezier::GetCurveOnVApproxLen( double dU) const
{
PtrOwner<CurveComposite> pCrvCo( GetCurveOnV( dU)) ;
double dLen ;
if ( IsNull( pCrvCo) || ! pCrvCo->GetApproxLength( dLen))
return 0 ;
return dLen ;
}
//----------------------------------------------------------------------------
const SurfTriMesh*
SurfBezier::GetAuxSurf( void) const
{
// la superficie deve essere validata
if ( m_nStatus != OK) {
ResetAuxSurf() ;
return nullptr ;
}
// se gi calcolata, la restituisco
if ( m_pSTM != nullptr)
return m_pSTM ;
// costruttore della superficie
POLYLINEMATRIX vvPL ;
//POLYLINEVECTOR vPL ; // per usare i polygon basic
Tree Tree( this, true) ;
BIPNTVECTOR vTrees ;
Tree.GetIndependentTrees( vTrees) ;
bool bTest = false ; // per debug
for ( int i = 0 ; i < (int) vTrees.size() ; ++ i) {
Point3d ptMin = std::get<0>( vTrees[i]) ;
Point3d ptMax = std::get<1>( vTrees[i]) ;
Tree.SetSurf( this, true, ptMin, ptMax) ;
if ( bTest) {
Tree.BuildTree_test() ; // per debug
//Tree.BuildTree( 5 * LIN_TOL_FINE, 1) ; // per debug
Tree.SetTestMode() ;
}
else {
Tree.BuildTree( 5 * LIN_TOL_FINE, 0.1) ;
}
Tree.GetPolygons( vvPL) ;
//Tree.GetPolygonsBasic( vPL) ; // per usare i polygon basic
}
//// per usare i polygon basic//////////////////////
//for (int k = 0 ; k < (int)vPL.size(); ++k) {
// vvPL.emplace_back() ;
// vvPL.back().push_back(vPL[k]) ;
//}
//// per usare i polygon basic///////////////////
// salvo i bordi in 3d, che servono in caso si voglia trimmare la superficie DOPO aver costruito la trimesh ausiliaria
Tree.GetEdge3D( m_vPLEdges) ;
// qui non sarebbe male stampare un messaggio di errore nel log se avevo un'area da disegnare ma non sono usciti dei poligoni
if ( int(vvPL.size()) == 0)
LOG_DBG_ERR( GetEGkLogger(), "ERROR : Bezier Surface couldn't be triangulated, hence wasn't drawn") ;
StmFromTriangleSoup stmSoup ;
if ( ! stmSoup.Start())
return nullptr ;
// prendo i punti di ogni polyline dell'albero, li triangolo e li porto in 3d
for ( POLYLINEVECTOR vPL : vvPL) {
PNTVECTOR vPnt ;
INTVECTOR vTria ;
Triangulate Tri ;
if ( ! Tri.Make( vPL, vPnt, vTria))
return nullptr ;
// porto i punti in 3d
PNTVECTOR vPnt3d ;
for ( int i = 0 ; i < int( vPnt.size()) ; ++ i) {
Point3d pt3d ;
if ( ! GetPointD1D2( vPnt[i].x / SBZ_TREG_COEFF, vPnt[i].y / SBZ_TREG_COEFF, ISurfBezier::FROM_MINUS, ISurfBezier::FROM_MINUS, pt3d))
return nullptr ;
vPnt3d.push_back( pt3d) ;
}
int nTria = int( vTria.size()) / 3 ;
for ( int i = 0 ; i < nTria ; ++i) {
if ( ! stmSoup.AddTriangle( vPnt3d[vTria[3*i]], vPnt3d[vTria[3*i+1]], vPnt3d[vTria[3*i+2]],
vPnt[vTria[3*i]].x, vPnt[vTria[3*i]].y,
vPnt[vTria[3*i+1]].x, vPnt[vTria[3*i+1]].y,
vPnt[vTria[3*i+2]].x, vPnt[vTria[3*i+2]].y))
return nullptr ;
}
}
// la salvo
if ( ! stmSoup.End())
return nullptr ;
m_pSTM = GetBasicSurfTriMesh( stmSoup.GetSurf()) ;
return m_pSTM ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::GetLeaves( vector<tuple<int, Point3d, Point3d>>& vLeaves) const
{
Tree Tree( this, true) ;
BIPNTVECTOR vTrees ;
Tree.GetIndependentTrees( vTrees) ;
for ( int i = 0 ; i < int( vTrees.size()) ; ++ i) {
Point3d ptMin = get<0>( vTrees[i]) ;
Point3d ptMax = get<1>( vTrees[i]) ;
Tree.SetSurf( this, true, ptMin, ptMax) ;
bool bTest = false ; // per debug
if ( bTest) {
Tree.BuildTree_test() ; // per debug
//Tree.BuildTree( 5 * LIN_TOL_FINE, 1) ; // per debug
}
else {
Tree.BuildTree( 5 * LIN_TOL_FINE, 0.1) ;
}
vector<Cell> vCells ;
Tree.GetLeaves( vCells) ;
for ( int k = 0 ; k < int( vCells.size()) ; ++ k) {
vLeaves.emplace_back( vCells[k].m_nId, vCells[k].GetBottomLeft(), vCells[k].GetTopRight()) ;
}
}
return true ;
}
//----------------------------------------------------------------------------
void
SurfBezier::ResetAuxSurf( void) const
{
if ( m_pSTM != nullptr)
delete( m_pSTM) ;
m_pSTM = nullptr ;
}
//----------------------------------------------------------------------------
void
SurfBezier::ResetTrimRegion( void)
{
if ( m_pTrimReg != nullptr)
delete( m_pTrimReg) ;
m_pTrimReg = nullptr ;
}
//----------------------------------------------------------------------------
ICurveComposite*
SurfBezier::UnprojectCurveFromStm( const ICurveComposite* pCC) const
{
// do per scontato che la compo sia una spezzata, visto che arriva dall'intersezione tra un piano e una trimesh
const ICurve* pCrv0 = pCC->GetCurve( 0) ;
PolyLine pl ;
Point3d pt3D, pt2D ; pCrv0->GetStartPoint( pt3D) ;
DistPointSurfTm distPtStm0( pt3D, *GetAuxSurf()) ;
// aggiungo il primo punto
int nTriaIndex ; distPtStm0.GetMinDistTriaIndex( nTriaIndex) ;
if ( ! UnprojectPointFromStm( nTriaIndex, pt3D, pt2D))
return nullptr ;
pl.AddUPoint( 0, pt2D) ;
// aggiungo tutti i successivi
for ( int i = 1 ; i < int( pCC->GetCurveCount()) ; ++i) {
const ICurve* pCrv = pCC->GetCurve( i) ;
pCrv->GetEndPoint( pt3D) ;
DistPointSurfTm distPtStm( pt3D, *GetAuxSurf()) ;
distPtStm.GetMinDistTriaIndex( nTriaIndex) ;
if ( ! UnprojectPointFromStm( nTriaIndex, pt3D, pt2D))
return nullptr ;
pl.AddUPoint( i, pt2D) ;
}
PtrOwner<ICurveComposite> pCC2D ( CreateCurveComposite()) ;
pCC2D->FromPolyLine( pl) ;
return Release( pCC2D) ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::Cut( const Plane3d& plPlane, bool bSaveOnEq)
{
// faccio l'intersezione della trimesh ausiliaria con il piano posso ottenere: punti, curve 3d e triangoli( coplanari al piano di taglio)
// i punti li escludo
// le curve 3d le trasformo in curve 2d e le aggiungo alle curve di trim
// accorpo eventuali triangoli adiacenti ed estraggo i loop delle regioni ottenute; questi vengono poi portati in 2d e aggiunti alle curve di trim
PNTVECTOR vPnt ;
BIPNTVECTOR vBPnt ;
TRIA3DVECTOR vTria ;
IntersPlaneSurfTm( plPlane, *GetAuxSurf(), vPnt, vBPnt, vTria) ;
// recupero la superficie di trim a cui devo aggiungere tutt i nuovi loop di trim
PtrOwner<SurfFlatRegion> sfrTrimReg ( GetTrimRegion()) ;
if ( IsNull( sfrTrimReg))
sfrTrimReg.Set( CreateBasicSurfFlatRegion()) ;
// concateno le curve 3d
ChainCurves chainC ;
double dToler = EPS_SMALL ;
chainC.Init( false, dToler, int( vBPnt.size())) ;
for ( int i = 0 ; i < int( vBPnt.size()) ; ++ i) {
Vector3d vtDir = vBPnt[i].second - vBPnt[i].first ;
vtDir.Normalize() ;
if ( ! chainC.AddCurve( i + 1, vBPnt[i].first, vtDir, vBPnt[i].second, vtDir))
return false ;
}
// GESTIONE DELLE CURVE OTTENUTE DALL'INTERSEZIONE
// recupero i percorsi concatenati
Point3d ptNear = ( vBPnt.empty() ? ORIG : vBPnt[0].first) ;
INTVECTOR vId ;
// Devo controllare se i trim intersecano il bordo dello spazio parametrico ed eventualmente spezzare le curve di trim in 2D tenendo conto della periodicità
// costruisco le curveCompo che rappresetano gli edge
ICRVCOMPOPVECTOR vCCEdge ;
for ( int i= 0 ; i < int( m_vPLEdges.size()); ++i) {
vCCEdge.emplace_back() ;
vCCEdge.back()->FromPolyLine( m_vPLEdges[i]) ;
}
// separo tra loop chiusi, interni allo spazio parametrico e loop passanti che tagliano lo spazio intersecando i bordi
ICRVCOMPOPOVECTOR vpCCOpen ;
ICRVCOMPOPOVECTOR vpCCClosed ;
while ( chainC.GetChainFromNear( ptNear, false, vId)) {
// creo una curva composita
PtrOwner<ICurveComposite> pCrvCompo( CreateCurveComposite()) ;
if ( IsNull( pCrvCompo))
return false ;
// recupero gli estremi dei segmenti, creo le linee e le inserisco nella composita
bool bAdded = true ;
for ( int i = 0 ; i < int( vId.size()) ; ++ i) {
// creo un segmento di retta
PtrOwner<ICurveLine> pLine( CreateCurveLine()) ;
if ( IsNull( pLine))
return false ;
// recupero gli estremi (non vanno mai invertiti per opzione di concatenamento)
int nInd = abs( vId[i]) - 1 ;
Point3d ptStart = ( bAdded ? vBPnt[nInd].first : ptNear) ;
Point3d ptEnd = vBPnt[nInd].second ;
// provo ad accodarlo alla composita
bAdded = ( Dist( ptStart, ptEnd) > dToler / 2 &&
pLine->Set( ptStart, ptEnd) &&
pCrvCompo->AddCurve( Release( pLine), true, dToler)) ;
ptNear = ( bAdded ? ptEnd : ptStart) ;
}
// se lunghezza curva inferiore a 5 volte la tolleranza, la ignoro e sposto il punto finale nel punto di fine della curva che sto ignorando
double dCrvLen ;
if ( ! pCrvCompo->GetLength( dCrvLen) || dCrvLen < 5. * dToler)
continue ;
// se curva chiusa entro 5 volte la tolleranza ma considerata aperta, la chiudo bene
Point3d ptStart, ptEnd ;
if ( pCrvCompo->GetStartPoint( ptStart) &&
pCrvCompo->GetEndPoint( ptEnd) &&
AreSamePointEpsilon( ptStart, ptEnd, 5. * dToler) &&
! AreSamePointApprox( ptStart, ptEnd)) {
// porto il punto finale a coincidere esattamente con l'inizio
pCrvCompo->ModifyEnd( ptStart) ;
}
// assegno estrusione come direzione normale al piano
pCrvCompo->SetExtrusion( plPlane.GetVersN()) ;
// unisco segmenti allineati
pCrvCompo->MergeCurves( 0.5 * dToler, ANG_TOL_STD_DEG) ;
// porto la curva nello spazio parametrico
PtrOwner<ICurveComposite> pCC( UnprojectCurveFromStm( pCrvCompo)) ;
// le curve aperte le tengo da parte per giuntarle alla fine col bordo
if ( ! pCC->IsClosed())
vpCCOpen.emplace_back( Release( pCC)) ;
// le curve chiuse le metto tutte insieme subito
else
vpCCClosed.emplace_back( Release( pCC)) ; // da controllare che funzioni anche se passo i loop con orientamenti sbagliati//////////
}
// creo il nuovo bordo usando i loop aperti
POLYLINEMATRIX vPolygons ;
if ( int(vpCCOpen.size()) != 0 ) {
Tree Tree(Point3d( 0, 0, 0), Point3d( m_nSpanU * SBZ_TREG_COEFF, m_nSpanV * SBZ_TREG_COEFF)) ;
POLYLINEVECTOR vPL ;
for ( int i = 0 ; i < int(vpCCOpen.size()); ++i ) {
PolyLine pl ;
vpCCOpen[i]->ApproxWithLines( LIN_TOL_FINE, ANG_TOL_STD_DEG, ICurveComposite::APL_STD, pl) ;
vPL.emplace_back( pl) ;
}
Tree.AddCutsToRoot( vPL) ;
Tree.CreateCellContour( vPolygons) ;
}
//GESTIONE DEI TRIANGOLI RISULTANTI DALL'INTERSEZIONE
StmFromTriangleSoup StmFts ;
if ( ! StmFts.Start())
return GDB_ID_NULL ;
for ( int i = 0 ; i < int( vTria.size()) ; ++ i)
// inserisco il triangolo nella nuova superficie
StmFts.AddTriangle( vTria[i]) ;
// valido la superficie e calcolo le adiacenze
if ( ! StmFts.End())
return GDB_ID_NULL ;
// se superficie con triangoli
PtrOwner<ISurfTriMesh> pNewStm( StmFts.GetSurf()) ;
POLYLINEVECTOR vPLTria ;
if ( ! IsNull( pNewStm) && pNewStm->GetTriangleCount() > 0) {
pNewStm->GetLoops( vPLTria) ;
}
SurfFlatRegionByContours sfrContour ;
// aggiungo i tagli sul bordo
for ( int i = 0 ; i < int( vPolygons.size()); ++i ) {
for ( int j = 0 ; j < int( vPolygons[i].size()); ++j) {
PtrOwner<ICurveComposite> pCC( CreateCurveComposite()) ;
pCC->FromPolyLine( vPolygons[i][j]) ;
sfrContour.AddCurve( Release( pCC)) ;
}
}
// aggiungo i loop chiusi
for ( int i = 0 ; i < int( vpCCClosed.size()); ++i )
sfrContour.AddCurve( Release( vpCCClosed[i])) ;
// aggiungo loop derivati dai triangoli
for ( int i = 0 ; i < int( vPLTria.size()); ++i ) {
PtrOwner<ICurveComposite> pCC( CreateCurveComposite()) ;
pCC->FromPolyLine( vPLTria[i]) ;
sfrContour.AddCurve( Release( pCC)) ;
}
PtrOwner<ISurfFlatRegion> pSFR( sfrContour.GetSurf()) ;
if ( IsNull( pSFR) || ! pSFR->IsValid())
return false ;
// se la superficie ha normale con z negativa la inverto
if ( pSFR->GetNormVersor().z < 0)
pSFR->Invert() ;
// verifico se la superficie che ho ottenuto è corretta o devo prendere il complementare ( rispetto allo spazio parametrico totale)
// per verificarlo prendo un punto su questa superficie e verifico dove sta il suo corrispettivo 3D rispetto al piano di taglio
int nChunkMin = 0 , nLoopMin = 0 ;
// sono nello spazio parametrico, quindi le aree delle curve possono essere molto grandi
double dAreaMin = 1e30 ;
bool bPos = false ;
for ( int c = 0 ; c < int( pSFR->GetChunkCount()); ++c) {
PtrOwner<ISurfFlatRegion> pSurf( pSFR->CloneChunk( c)) ;
for ( int l = 0 ; l < pSurf->GetLoopCount( 0); ++l) {
PtrOwner<ICurve> pCrv( pSurf->GetLoop( 0, l)) ;
double dArea ; pCrv->GetAreaXY( dArea) ;
if ( abs( dArea) < dAreaMin) {
nChunkMin = c ;
nLoopMin = l ;
dAreaMin = abs( dArea) ;
bPos = dArea > 0 ;
}
}
}
// aggiorno la superficie di trim
Point3d ptStart ;
Vector3d vtDir ;
PtrOwner<ICurve> pCrv( pSFR->GetLoop( nChunkMin, nLoopMin)) ;
pCrv->GetStartPoint( ptStart) ;
pCrv->GetStartDir( vtDir) ;
vtDir.Rotate( Z_AX, bPos? 90 : -90) ;
PtrOwner<ICurveLine> pCL( CreateCurveLine()) ;
pCL->SetPVL( ptStart, vtDir, 1e6) ;
IntersCurveCurve icc( *Release( pCL), *Release(pCrv)) ;
// verifico di guardare verso l'interno ( il numero di intersezioni deve essere pari visto che partivo da un punto sulla curva)
if ( icc.GetIntersCount()%2 != 0)
return false ; // o sennò un altro tentativo prima di rinunciare
IntCrvCrvInfo iccInfo ;
icc.GetIntCrvCrvInfo( 1, iccInfo) ;
Point3d ptI = iccInfo.IciA[0].ptI ;
Point3d ptToCheck = ( ptStart + ptI) / 2 ;
Point3d pt3D ; GetPointD1D2( ptToCheck.x / SBZ_TREG_COEFF, ptToCheck.y / SBZ_TREG_COEFF, ISurfBezier::FROM_MINUS, ISurfBezier::FROM_MINUS, pt3D) ;
double dDist = DistPointPlane( pt3D, plPlane) ;
// ho due casi in cui devo invertire(prendere il complementare rispetto allo spazio parametrico) la superficie:
// 1. se il punto è sopra il piano ed era dentro una curva CCW
// 2. se il punto è sotto il piano ed era interno ad una curva CW
PtrOwner<ISurfFlatRegion> pSrfFR_Copy( pSFR->Clone()) ;
SaveGeoObj( Release(pSrfFR_Copy), "D:\\Temp\\inters\\failed_trim.nge", GDB_SV_BIN) ;
if ( ( dDist > 0 && bPos) || ( dDist < 0 && ! bPos))
SetTrimRegion( *Release( pSFR), false) ;
else
SetTrimRegion( *Release( pSFR)) ;
return true ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::UnprojectPointFromStm( int nT, const Point3d& ptI, Point3d& ptSP, int nIL) const
{
// dato un punto sulla trimesh ausiliaria, ne ricavo le coordinate parametriche
const ISurfTriMesh* pSurfTm = GetAuxSurf() ;
// recupero i dati dei vertici del triangolo che fa intersezione
int nVert[3] ;
pSurfTm->GetTriangle( nT, nVert) ;
double dU0, dV0, dU1, dV1,dU2, dV2 ;
pSurfTm->GetVertexParam( nVert[0], dU0, dV0) ;
pSurfTm->GetVertexParam( nVert[1], dU1, dV1) ;
pSurfTm->GetVertexParam( nVert[2], dU2, dV2) ;
Point3d pt0, pt1, pt2 ;
pSurfTm->GetVertex( nVert[0], pt0) ;
pSurfTm->GetVertex( nVert[1], pt1) ;
pSurfTm->GetVertex( nVert[2], pt2) ;
// se l'intersezione era su un vertice restituisco le coordinate parametriche del vertice
if ( nIL == 3 ) {
if ( AreSamePointApprox(ptI, pt0))
ptSP.Set( dU0, dV0, 0) ;
else if ( AreSamePointApprox(ptI, pt1))
ptSP.Set( dU1, dV1, 0) ;
else if ( AreSamePointApprox(ptI, pt2))
ptSP.Set( dU2, dV2, 0) ;
return true ;
}
// calcolo approssimativamente le coordinate nello spazio parametrico del punto di intersezione
// quindi prima calcolo la composizione lineare tra i vertici del triangolo per ottenere il punto di intersezione
Eigen::Matrix3d mA ;
mA.col(0) << pt0.x, pt0.y , pt0.z ;
mA.col(1) << pt1.x, pt1.y , pt1.z ;
mA.col(2) << pt2.x, pt2.y , pt2.z ;
Eigen::Vector3d b ( ptI.x, ptI.y, ptI.z) ;
Eigen::Vector3d x = mA.fullPivLu().solve(b) ;
// applico questa composizione alle loro coordinate parametriche
Eigen::Matrix3d mB ;
mB.col(0) << dU0, dV0, 0 ;
mB.col(1) << dU1, dV1, 0 ;
mB.col(2) << dU2, dV2, 0 ;
Eigen::Vector3d ptParam = mB * x ;
ptSP.x = ptParam.x() ;
ptSP.y = ptParam.y() ;
return true ;
}
//----------------------------------------------------------------------------
bool
SurfBezier::UnprojectPoint( const Point3d& pt3D, Point3d& ptParam) const
{
// dato il punto pt3D sulla superficie di Bezier si cercano le coordinate parametriche ( ptParam) , iterativamente con Newton
// trovato un primo candidato ptParam, ne calcolo l'immagine sulla superficie ( ptBez) e ne calcolo la distanza con il punto pt3D
// ripeto cercando di avvicinarmi il più possibile
// per trovare il primo punto trovo il triangolo della trimesh ausiliaria più vicino e il punto più vicino
DistPointSurfTm dptSurfTm( pt3D, *GetAuxSurf()) ;
int nTriaIndex ; dptSurfTm.GetMinDistTriaIndex( nTriaIndex) ;
Point3d ptI ; dptSurfTm.GetMinDistPoint( ptI) ;
UnprojectPointFromStm( nTriaIndex, ptI, ptParam) ;
Point3d ptBez ;
GetPointD1D2( ptParam.x / SBZ_TREG_COEFF, ptParam.y / SBZ_TREG_COEFF, ISurfBezier::FROM_MINUS, ISurfBezier::FROM_MINUS, ptBez) ;
// usando un algoritmo di newton cerco di avvicinarmi il più possibile al punto
double dDistNew = Dist( pt3D, ptBez) ;
double dDistPre = dDistNew ;
int nCount = 0 ;
double dh = EPS_SMALL ;
// metodo di newton in più dimensioni
// vario sia il parametro U che il parametro V e verifico se la distanza dalla retta diminuisce per scostamenti positivi o negativi.
while ( dDistNew > EPS_SMALL && nCount < 100) {
dDistPre = dDistNew ;
Point3d ptIBzNew1 ;
GetPointD1D2( ( ptParam.x + dh) / SBZ_TREG_COEFF, ptParam.y / SBZ_TREG_COEFF, ISurfBezier::FROM_MINUS, ISurfBezier::FROM_MINUS, ptIBzNew1) ;
dDistNew = Dist( pt3D, ptIBzNew1) ;
double dfdU = ( dDistNew - dDistPre) / dh ;
Point3d ptIBzNew2 ;
GetPointD1D2( ptParam.x / SBZ_TREG_COEFF, ( ptParam.y + dh) / SBZ_TREG_COEFF, ISurfBezier::FROM_MINUS, ISurfBezier::FROM_MINUS, ptIBzNew2) ;
dDistNew = Dist( pt3D, ptIBzNew2) ;
double dfdV = ( dDistNew - dDistPre) / dh ;
//// opzione 0
////scelgo h1 e h2 separatamente e in modo da annullare f(x)
//// opzione 1
//// valore fisso
//double dr = EPS_SMALL ;
//if ( dDistPre > 1)
// dr = 1 ;
//else if ( dDistPre > 0.1)
// dr = 0.1 ;
//else if ( dDistPre > 0.01)
// dr = 0.01 ;
//// opzione 2
//// valore direttamente vincolato
//double dr = dDistPre ;
//// opzione 3
//// valuto la deformazione locale in base allo spostamento del punto sulla bezier // non serve
//double dh1 = Dist( ptIBz, ptIBzNew1) ;
//double dh2 = Dist( ptIBz, ptIBzNew2) ;
// potrei valutare il nuovo spostamento in base all'ultima variazione di dDist
// potrei anche vedere se sto uscendo dal triangolo ( definito nello spazio parametrico)
// mi avvicino cercando di annullare la distanza in un colpo solo
double dr = - dDistPre / ( dfdU + dfdV) ;
GetPointD1D2(( ptParam.x + dr * dfdU) / SBZ_TREG_COEFF, ( ptParam.y + dr * dfdV) / SBZ_TREG_COEFF, ISurfBezier::FROM_MINUS, ISurfBezier::FROM_MINUS, ptBez) ;
dDistNew = Dist( pt3D, ptBez) ;
++nCount ;
}
return nCount != 99 ;
}