Skip to content

Commit

Permalink
plugin work
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioJerez committed Oct 19, 2024
1 parent 5a37c65 commit 0d4660f
Show file tree
Hide file tree
Showing 24 changed files with 430 additions and 263 deletions.
9 changes: 9 additions & 0 deletions newton-4.00/applications/ndSandbox/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ int main(int, char**)
//index.SetCount(xxxx.GetCount());
//ndInt32 vertexCount = ndVertexListToIndexList(&xxxx[0].m_x, sizeof(ndVector), 3, ndInt32(xxxx.GetCount()), &index[0], ndFloat32(1.0e-6f));

//ndVector points[5];
//points[0] = ndVector(-1.0f, -1.0f, 0.0f, 0.0f);
//points[1] = ndVector( 1.0f, -1.0f, 0.0f, 0.0f);
//points[2] = ndVector(1.2f, 0.6f, 0.0f, 0.0f);
//points[3] = ndVector(0.9f, 0.9f, 0.0f, 0.0f);
//points[4] = ndVector(-1.0f, 1.0f, 0.0f, 0.0f);
//ndArray<ndInt32> triangles;
//ndTriangulatePolygon(points, 5, triangles);

ndDemoEntityManager demos;
demos.Run();
return 0;
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<ClCompile Include="Public\dCore\ndThreadPool.cpp" />
<ClCompile Include="Public\dCore\ndThreadSyncUtils.cpp" />
<ClCompile Include="Public\dCore\ndTree.cpp" />
<ClCompile Include="Public\dCore\ndTriangulatePolygon.cpp" />
<ClCompile Include="Public\dCore\ndTypes.cpp" />
<ClCompile Include="Public\dCore\ndUtils.cpp" />
<ClCompile Include="Public\dCore\ndVector.cpp" />
Expand Down Expand Up @@ -257,6 +258,7 @@
<ClInclude Include="Public\dCore\ndThreadPool.h" />
<ClInclude Include="Public\dCore\ndThreadSyncUtils.h" />
<ClInclude Include="Public\dCore\ndTree.h" />
<ClInclude Include="Public\dCore\ndTriangulatePolygon.h" />
<ClInclude Include="Public\dCore\ndTypes.h" />
<ClInclude Include="Public\dCore\ndUtils.h" />
<ClInclude Include="Public\dCore\ndVector.h" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,9 @@
<ClCompile Include="Public\thirdParty\ndConvexApproximation.cpp">
<Filter>thirdParty</Filter>
</ClCompile>
<ClCompile Include="Public\dCore\ndTriangulatePolygon.cpp">
<Filter>dCore</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Public\dCore\ndAabbPolygonSoup.h">
Expand Down Expand Up @@ -869,6 +872,9 @@
<ClInclude Include="Public\thirdParty\ndConvexApproximation.h">
<Filter>thirdParty</Filter>
</ClInclude>
<ClInclude Include="Public\dCore\ndTriangulatePolygon.h">
<Filter>dCore</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Natvis Include="Public\dCore\ndTypes.natvis">
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
#include <ndAabbPolygonSoup.h>
#include <ndSmallDeterminant.h>
#include <ndConjugateGradient.h>
#include <ndTriangulatePolygon.h>
#include <ndPolygonSoupBuilder.h>
#include <ndPolygonSoupDatabase.h>
#include <tinyxml/ndTinyXmlGlue.h>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* Copyright (c) <2003-2022> <Julio Jerez, Newton Game Dynamics>
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*/

#include "ndCoreStdafx.h"
#include "ndPolyhedra.h"
#include "ndTriangulatePolygon.h"

void ndTriangulatePolygon(ndVector* const points, ndInt32 count, ndArray<ndInt32>& triangles)
{
triangles.SetCount(0);
if (count < 3)
{
return;
}
if (count == 3)
{
triangles.PushBack(0);
triangles.PushBack(1);
triangles.PushBack(2);
return;
}

ndInt32 index[256];
ndFixSizeArray<ndBigVector, 256> vertexBuffer;
for (ndInt32 i = 0; i < count; ++i)
{
index[i] = i;
vertexBuffer.PushBack(points[i]);
}

ndPolyhedra polyhedra;
polyhedra.BeginFace();
polyhedra.AddFace(count, index);
polyhedra.EndFace();

ndPolyhedra leftOversOut;
polyhedra.Triangulate(&vertexBuffer[0].m_x, sizeof(ndBigVector), &leftOversOut);

ndInt32 mark = polyhedra.IncLRU();
ndPolyhedra::Iterator it(polyhedra);
for (it.Begin(); it; it++)
{
ndEdge* const edge = &it.GetNode()->GetInfo();
if (edge->m_mark != mark && (edge->m_incidentFace > 0))
{
ndAssert(edge == edge->m_next->m_next->m_next);
edge->m_mark = mark;
edge->m_next->m_mark = mark;
edge->m_next->m_next->m_mark = mark;
triangles.PushBack(edge->m_incidentVertex);
triangles.PushBack(edge->m_next->m_incidentVertex);
triangles.PushBack(edge->m_next->m_next->m_incidentVertex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* Copyright (c) <2003-2022> <Julio Jerez, Newton Game Dynamics>
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*/

#ifndef __ND_TRIANGULATE_POLYGON_H__
#define __ND_TRIANGULATE_POLYGON_H__

#include "ndCoreStdafx.h"
#include "ndArray.h"
#include "ndVector.h"

D_CORE_API void ndTriangulatePolygon(ndVector* const vertexCloud2d, ndInt32 count, ndArray<ndInt32>& triangles);

#endif
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ UNewtonCollision::UNewtonCollision()

BestFit = false;
CastShadow = 0;
m_savedMeshComponent = nullptr;
bExplicitShowWireframe = true;
m_visualMesh = TSharedPtr<UE::Geometry::FDynamicMesh3>(nullptr);

Expand All @@ -45,13 +44,34 @@ void UNewtonCollision::OnUnregister()
m_shape = nullptr;
}
m_hash = 0;
m_savedMeshComponent = nullptr;
m_visualMesh = TSharedPtr<UE::Geometry::FDynamicMesh3>(nullptr);
}

void UNewtonCollision::SetGeometryMesh(USceneComponent* const staticMesh)
//void UNewtonCollision::SetGeometryMesh(USceneComponent* const staticMesh)
//{
// m_savedMeshComponent = staticMesh;
//}

void UNewtonCollision::SetTransform(const USceneComponent* const meshComponent)
{
m_savedMeshComponent = staticMesh;
const AActor* const owner = GetOwner();
check(owner);
const FTransform bodyTransform(owner->GetRootComponent()->GetComponentToWorld());
const FTransform globalTransform(meshComponent->GetComponentToWorld());
const FTransform localTransform(globalTransform * bodyTransform.Inverse());

SetComponentToWorld(globalTransform);

// for some reason, this does not work in the unreal editor
//SetRelativeTransform(localTransform);
SetRelativeScale3D_Direct(localTransform.GetScale3D());
SetRelativeRotation_Direct(FRotator(localTransform.GetRotation()));
SetRelativeLocation_Direct(localTransform.GetLocation());
}

void UNewtonCollision::InitStaticMeshCompoment(const USceneComponent* const meshComponent)
{
check(0);
}

void UNewtonCollision::PostLoad()
Expand Down Expand Up @@ -184,11 +204,13 @@ void UNewtonCollision::ApplyPropertyChanges()
m_points.PushBack(faceArray[i] * m_scale);
}

for (ndInt32 i = 2; i < vertexCount; ++i)
ndArray<ndInt32> triangles;
ndTriangulatePolygon(&m_points[0], vertexCount, triangles);
for (ndInt32 i = 0; i < ndInt32 (triangles.GetCount()); i+=3)
{
m_index.PushBack(baseIndex);
m_index.PushBack(baseIndex + i - 0);
m_index.PushBack(baseIndex + i - 1);
m_index.PushBack(baseIndex + triangles[i + 0]);
m_index.PushBack(baseIndex + triangles[i + 1]);
m_index.PushBack(baseIndex + triangles[i + 2]);
}
}

Expand Down Expand Up @@ -290,39 +312,22 @@ void UNewtonCollision::ApplyPropertyChanges()
}
}

UStaticMesh* UNewtonCollision::FindStaticMesh() const
{
if (m_savedMeshComponent)
{
UStaticMeshComponent* const staticMesh = Cast<UStaticMeshComponent>(m_savedMeshComponent);
return staticMesh ? staticMesh->GetStaticMesh().Get() : (UStaticMesh*) nullptr;
}

const UStaticMeshComponent* const mesh = Cast<UStaticMeshComponent>(GetAttachParent());
if (mesh && mesh->GetStaticMesh().Get())
{
return mesh->GetStaticMesh().Get();
}

return nullptr;
}

void UNewtonCollision::SetGlobalTransform()
{
UStaticMesh* const staticMesh = FindStaticMesh();
if (staticMesh && m_savedMeshComponent)
{
//const ANewtonSceneActor* const owner = Cast<ANewtonSceneActor>(GetOwner());
const AActor* const owner = GetOwner();
check(owner);
const FTransform bodyTransform(owner->GetRootComponent()->GetComponentToWorld());
const FTransform globalTransform(m_savedMeshComponent->GetComponentToWorld());
const FTransform localTransform(globalTransform * bodyTransform.Inverse());

SetRelativeTransform(localTransform);
SetComponentToWorld(globalTransform);
}
}
//UStaticMesh* UNewtonCollision::FindStaticMesh() const
//{
// if (m_savedMeshComponent)
// {
// UStaticMeshComponent* const staticMesh = Cast<UStaticMeshComponent>(m_savedMeshComponent);
// return staticMesh ? staticMesh->GetStaticMesh().Get() : (UStaticMesh*) nullptr;
// }
//
// const UStaticMeshComponent* const mesh = Cast<UStaticMeshComponent>(GetAttachParent());
// if (mesh && mesh->GetStaticMesh().Get())
// {
// return mesh->GetStaticMesh().Get();
// }
//
// return nullptr;
//}

ndVector UNewtonCollision::GetVolumePosition() const
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ class UNewtonCollision : public UDynamicMeshComponent
public:
// Sets default values for this component's properties
UNewtonCollision();

virtual void SetGlobalTransform();
virtual ndVector GetVolumePosition() const;
void SetGeometryMesh(USceneComponent* const staticMesh);
virtual void SetWireFrameColor(const FLinearColor& color);
virtual void InitStaticMeshCompoment(const USceneComponent* const meshComponent);

protected:
UPROPERTY(EditAnywhere, Category = Newton)
Expand All @@ -43,20 +41,19 @@ class UNewtonCollision : public UDynamicMeshComponent
virtual void BeginDestroy() override;
virtual bool ShouldCreatePhysicsState() const override;
virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;

virtual void BuildNewtonShape();
virtual void ApplyPropertyChanges();
virtual UStaticMesh* FindStaticMesh() const;
//virtual UStaticMesh* FindStaticMesh() const;

virtual ndShape* CreateShape() const;
virtual long long CalculateHash() const;
virtual ndShapeInstance* CreateInstanceShape() const;
virtual ndShapeInstance* CreateBodyInstanceShape(const ndMatrix& bodyMatrix) const;
void SetTransform(const USceneComponent* const meshComponent);

//public:
long long m_hash;
ndShape* m_shape;
USceneComponent* m_savedMeshComponent;
TSharedPtr<UE::Geometry::FDynamicMesh3> m_visualMesh;
UMaterial* m_transparentMaterial;
bool m_propertyChanged;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,34 @@ ndShape* UNewtonCollisionBox::CreateShape() const
return new ndShapeBox(SizeX * UNREAL_INV_UNIT_SYSTEM, SizeY * UNREAL_INV_UNIT_SYSTEM, SizeZ * UNREAL_INV_UNIT_SYSTEM);
}

void UNewtonCollisionBox::InitStaticMeshCompoment(const USceneComponent* const meshComponent)
{
const UStaticMeshComponent* const staticMeshComponent = Cast<UStaticMeshComponent>(meshComponent);
check(staticMeshComponent);
const UStaticMesh* const staticMesh = staticMeshComponent->GetStaticMesh().Get();
const UBodySetup* const bodySetup = staticMesh->GetBodySetup();
const FKAggregateGeom& aggGeom = bodySetup->AggGeom;
check(aggGeom.BoxElems.Num() == 1);
const FKBoxElem& box = aggGeom.BoxElems[0];

SetTransform(meshComponent);
const FTransform localTransformOffset(box.GetTransform());
const FTransform globalTransform(localTransformOffset * GetComponentToWorld());
SetComponentToWorld(globalTransform);

const AActor* const owner = GetOwner();
const FTransform bodyTransform(owner->GetRootComponent()->GetComponentToWorld());
const FTransform localTransform(globalTransform * bodyTransform.Inverse());

SetRelativeScale3D_Direct(localTransform.GetScale3D());
SetRelativeRotation_Direct(FRotator(localTransform.GetRotation()));
SetRelativeLocation_Direct(localTransform.GetLocation());

SizeX = box.X;
SizeY = box.Y;
SizeZ = box.Z;
}

long long UNewtonCollisionBox::CalculateHash() const
{
long long hash = ndCRC64(ndShapeBox::StaticClassName(), strlen(ndShapeBox::StaticClassName()), 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class UNewtonCollisionBox : public UNewtonCollision
public:
// Sets default values for this component's properties
UNewtonCollisionBox();
virtual void InitStaticMeshCompoment(const USceneComponent* const meshComponent) override;

protected:
virtual void ApplyPropertyChanges();
Expand Down
Loading

0 comments on commit 0d4660f

Please sign in to comment.