Skip to content

Commit 3b53c98

Browse files
authored
[src] Clean SofaContext code, remove some logs and reactive some methods (#27)
1 parent 48dac32 commit 3b53c98

1 file changed

Lines changed: 53 additions & 45 deletions

File tree

Source/SofaUE5/Private/SofaContext.cpp

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@
2525
#include "SofaUE5.h"
2626
#include "Engine.h"
2727
#include "CoreMinimal.h"
28-
//#include "SofaVisualMesh.h"
2928
#include "DAGNode/SofaDAGNode.h"
3029
#include "Base/SofaBaseComponent.h"
3130
#include <vector>
3231
#include <string>
3332

34-
//#include "SofaUE5Library/SofaPhysicsAPI.h"
3533
#include "SofaUE5Library/SofaAdvancePhysicsAPI.h"
3634

3735

@@ -55,7 +53,7 @@ ASofaContext::ASofaContext()
5553
m_log = true;
5654

5755
if (m_log && !(this->GetFlags() & RF_Transient))
58-
UE_LOG(SUnreal_log, Warning, TEXT("######### ASofaContext::ASofaContext(): %s | %s ##########"), *this->GetName(), *LexToString(this->GetFlags()));
56+
UE_LOG(SUnreal_log, Log, TEXT("######### ASofaContext::ASofaContext(): %s | %s ##########"), *this->GetName(), *LexToString(this->GetFlags()));
5957
}
6058

6159

@@ -65,7 +63,9 @@ void ASofaContext::OnConstruction(const FTransform& Transform)
6563
return;
6664
}
6765

68-
UE_LOG(SUnreal_log, Warning, TEXT("######### ASofaContext::OnConstruction(): %s | %s ##########"), *this->GetName(), *LexToString(this->GetFlags()));
66+
if (m_log)
67+
UE_LOG(SUnreal_log, Log, TEXT("######### ASofaContext::OnConstruction(): %s | %s ##########"), *this->GetName(), *LexToString(this->GetFlags()));
68+
6969
Super::OnConstruction(Transform);
7070

7171
#if WITH_EDITOR
@@ -83,26 +83,29 @@ void ASofaContext::Destroyed()
8383
}
8484

8585
if (m_log)
86-
UE_LOG(SUnreal_log, Warning, TEXT("######### ASofaContext::Destroyed(): %s | %s ##########"), *this->GetName(), *LexToString(this->GetFlags()));
86+
UE_LOG(SUnreal_log, Log, TEXT("######### ASofaContext::Destroyed(): %s | %s ##########"), *this->GetName(), *LexToString(this->GetFlags()));
8787

8888
// Remove UE5 children actor first before deleting SOFA context
8989
clearNodeGraph();
9090

91-
if (m_sofaAPI)
91+
if (m_sofaAPI.IsValid())
9292
{
93-
if (m_log)
94-
UE_LOG(SUnreal_log, Warning, TEXT("######### ASofaContext::Destroyed(): Delete SofaAdvancePhysicsAPI: %s"), *this->GetName());
95-
93+
// First stop SOFA simulation
94+
m_sofaAPI->stop();
95+
9696
if (m_isMsgHandlerActivated == true)
9797
catchSofaMessages();
9898

99-
UE_LOG(SUnreal_log, Warning, TEXT("## ASofaContext::BeginDestroy: m_sofaAPI stop"));
100-
m_sofaAPI->stop();
99+
if (m_log)
100+
UE_LOG(SUnreal_log, Log, TEXT("## ASofaContext::BeginDestroy: m_sofaAPI stopped"));
101+
102+
// Deactivate message handler
101103
m_sofaAPI->activateMessageHandler(false);
102-
UE_LOG(SUnreal_log, Warning, TEXT("## ASofaContext::BeginDestroy: m_sofaAPI stopped"));
103-
//delete m_sofaAPI;
104-
m_sofaAPI = nullptr;
105-
UE_LOG(SUnreal_log, Warning, TEXT("## ASofaContext::BeginDestroy: m_sofaAPI deleted"));
104+
// Free SOFA context Ptr
105+
m_sofaAPI.Reset();
106+
107+
if (m_log)
108+
UE_LOG(SUnreal_log, Log, TEXT("## ASofaContext::BeginDestroy: m_sofaAPI deleted"));
106109
}
107110

108111
Super::Destroyed();
@@ -115,8 +118,8 @@ void ASofaContext::BeginPlay()
115118
{
116119
if (m_log)
117120
{
118-
UE_LOG(SUnreal_log, Warning, TEXT("######### ASofaContext::BeginPlay(): %s | %s ##########"), *this->GetName(), *LexToString(this->GetFlags()));
119-
UE_LOG(SUnreal_log, Warning, TEXT("######### ASofaContext::BeginPlay(): %d ##########"), m_status);
121+
UE_LOG(SUnreal_log, Log, TEXT("######### ASofaContext::BeginPlay(): %s | %s ##########"), *this->GetName(), *LexToString(this->GetFlags()));
122+
UE_LOG(SUnreal_log, Log, TEXT("######### ASofaContext::BeginPlay(): %d ##########"), m_status);
120123
}
121124

122125
if (m_sofaAPI == nullptr)
@@ -126,7 +129,10 @@ void ASofaContext::BeginPlay()
126129

127130
if (m_sofaAPI)
128131
{
129-
UE_LOG(SUnreal_log, Warning, TEXT("## ASofaContext::BeginPlay: m_sofaAPI start"));
132+
if (m_log)
133+
UE_LOG(SUnreal_log, Log, TEXT("## ASofaContext::BeginPlay: m_sofaAPI start"));
134+
135+
// Start SOFA simulation on UE play
130136
m_sofaAPI->start();
131137
}
132138
else
@@ -140,39 +146,38 @@ void ASofaContext::BeginPlay()
140146
void ASofaContext::EndPlay(const EEndPlayReason::Type EndPlayReason)
141147
{
142148
if (m_log)
143-
UE_LOG(SUnreal_log, Warning, TEXT("######### ASofaContext::EndPlay(): %s | %s ##########"), *this->GetName(), *LexToString(this->GetFlags()));
149+
UE_LOG(SUnreal_log, Log, TEXT("######### ASofaContext::EndPlay(): %s | %s ##########"), *this->GetName(), *LexToString(this->GetFlags()));
144150

145151
if (m_sofaAPI)
146152
{
153+
// Stop SOFA simulation on UE play
147154
m_sofaAPI->stop();
148-
//m_sofaAPI->activateMessageHandler(false);
155+
156+
if (m_isMsgHandlerActivated == true)
157+
catchSofaMessages();
149158
}
159+
150160
Super::EndPlay(EndPlayReason);
151161
}
152162

153163

154-
155-
156-
157-
158-
159164
void ASofaContext::setDT(float value)
160165
{
161-
if (m_sofaAPI)
162-
m_sofaAPI->setTimeStep(value);
166+
//if (m_sofaAPI)
167+
// m_sofaAPI->setTimeStep(value);
163168
}
164169

165170
void ASofaContext::setGravity(FVector value)
166171
{
167-
if (m_sofaAPI)
168-
{
169-
UE_LOG(SUnreal_log, Warning, TEXT("## ASofaContext::setGravity: %f, %f, %f"), value.X, value.Y, value.Z);
170-
double* grav = new double[3];
171-
grav[0] = value.X;
172-
grav[1] = value.Y;
173-
grav[2] = value.Z;
174-
m_sofaAPI->setGravity(grav);
175-
}
172+
//if (m_sofaAPI)
173+
//{
174+
// UE_LOG(SUnreal_log, Warning, TEXT("## ASofaContext::setGravity: %f, %f, %f"), value.X, value.Y, value.Z);
175+
// double* grav = new double[3];
176+
// grav[0] = value.X;
177+
// grav[1] = value.Y;
178+
// grav[2] = value.Z;
179+
// m_sofaAPI->setGravity(grav);
180+
//}
176181
}
177182

178183

@@ -209,14 +214,12 @@ void ASofaContext::Tick( float DeltaTime )
209214
{
210215
if (m_status != -1 && m_sofaAPI)
211216
{
212-
//UE_LOG(LogTemp, Warning, TEXT("## ASofaContext: Tick %d"), m_status);
217+
// Step SOFA simulation on each UE tick
213218
m_sofaAPI->step();
214-
215-
double stime = m_sofaAPI->getTime();
216219

217-
//if (m_isMsgHandlerActivated == true)
218-
// catchSofaMessages();
219220
float value = this->GetGameTimeSinceCreation();
221+
//double stime = m_sofaAPI->getTime();
222+
220223
//UE_LOG(LogTemp, Warning, TEXT("## ASofaContext: Tick: %f %f"), value, stime);
221224
}
222225

@@ -242,7 +245,8 @@ void ASofaContext::createSofaContext()
242245
if (m_sofaAPI == nullptr)
243246
{
244247
m_sofaAPI = MakeShared<SofaAdvancePhysicsAPI>();
245-
UE_LOG(SUnreal_log, Warning, TEXT("## ASofaDAGNode::loadComponents TEST 12"));
248+
249+
UE_LOG(SUnreal_log, Warning, TEXT("## ASofaDAGNode::loadComponents TEST 28"));
246250

247251
if (m_sofaAPI == nullptr)
248252
{
@@ -253,8 +257,8 @@ void ASofaContext::createSofaContext()
253257
// activate message handler
254258
m_sofaAPI->activateMessageHandler(m_isMsgHandlerActivated);
255259

260+
// Test api Name
256261
m_apiName = m_sofaAPI->APIName();
257-
258262
if (m_log)
259263
{
260264
UE_LOG(SUnreal_log, Log, TEXT("## ASofaContext::createSofaContext: API Name: %s"), *m_apiName);
@@ -448,6 +452,9 @@ void ASofaContext::loadNodeGraph()
448452
}
449453
}
450454

455+
if (m_isMsgHandlerActivated == true)
456+
catchSofaMessages();
457+
451458
m_status++;
452459
}
453460

@@ -463,6 +470,9 @@ void ASofaContext::loadSofaComponents()
463470
dagNode->loadComponents(m_sofaAPI);
464471
}
465472
}
473+
474+
if (m_isMsgHandlerActivated == true)
475+
catchSofaMessages();
466476
}
467477

468478

@@ -536,8 +546,6 @@ void ASofaContext::catchSofaMessages()
536546
std::string rawMsg;
537547
for (int i = 0; i < nbrMsgs; ++i)
538548
{
539-
540-
//const char* rawMsg = m_sofaAPI->getMessage(i, *type).c_str();
541549
m_sofaAPI->getMessage_out(i, *type, rawMsg);
542550
FString FMessage(rawMsg.c_str());
543551

0 commit comments

Comments
 (0)