<commit_before>// This file is a part of the OpenSurgSim project.
// Copyright 2016, SimQuest Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

///\file RenderTestCcdSuture.cpp render test for ccd with Fem1D

#include <memory>

#include "SurgSim/Blocks/TransferPhysicsToVerticesBehavior.h"
#include "SurgSim/Blocks/VisualizeConstraints.h"
#include "SurgSim/Collision/Representation.h"
#include "SurgSim/Framework/BasicSceneElement.h"
#include "SurgSim/Framework/Log.h"
#include "SurgSim/Framework/SceneElement.h"
#include "SurgSim/Graphics/OsgCurveRepresentation.h"
#include "SurgSim/Graphics/OsgMeshRepresentation.h"
#include "SurgSim/Math/LinearSparseSolveAndInverse.h"
#include "SurgSim/Math/MeshShape.h"
#include "SurgSim/Math/OdeSolver.h"
#include "SurgSim/Math/RigidTransform.h"
#include "SurgSim/Math/SegmentMeshShape.h"
#include "SurgSim/Math/Vector.h"
#include "SurgSim/Physics/DeformableCollisionRepresentation.h"
#include "SurgSim/Physics/Fem1DElementBeam.h"
#include "SurgSim/Physics/Fem1DRepresentation.h"
#include "SurgSim/Physics/PhysicsManager.h"
#include "SurgSim/Physics/RenderTests/RenderTest.h"
#include "SurgSim/Physics/RigidCollisionRepresentation.h"
#include "SurgSim/Physics/RigidRepresentation.h"


namespace
{
std::shared_ptr<SurgSim::Framework::SceneElement> makeSuture(const std::string& filename)
{
	auto element = std::make_shared<SurgSim::Framework::BasicSceneElement>("Suture");

	// Physics
	auto physics = std::make_shared<SurgSim::Physics::Fem1DRepresentation>("Physics");
	physics->setFemElementType("SurgSim::Physics::Fem1DElementBeam");
	physics->setLocalPose(SurgSim::Math::RigidTransform3d::Identity());
	physics->loadFem(filename);
	physics->setIntegrationScheme(SurgSim::Math::INTEGRATIONSCHEME_EULER_IMPLICIT);
	physics->setLinearSolver(SurgSim::Math::LINEARSOLVER_LU);
	physics->setRayleighDampingMass(5.0);
	physics->setRayleighDampingStiffness(0.001);
	physics->setIsGravityEnabled(true);
	element->addComponent(physics);

	// Graphics
	auto gfx = std::make_shared<SurgSim::Graphics::OsgCurveRepresentation>("Graphics");
	gfx->setColor(SurgSim::Math::Vector4d(0.0, 0.0, 1.0, 1.0));
	gfx->setAntiAliasing(true);
	gfx->setWidth(0.9);
	element->addComponent(gfx);

	auto collision = std::make_shared<SurgSim::Physics::DeformableCollisionRepresentation>("Collision");
	auto shape = std::make_shared<SurgSim::Math::SegmentMeshShape>();
	shape->load(filename);
	shape->setRadius(0.0001);
	collision->setShape(shape);
	collision->setCollisionDetectionType(SurgSim::Collision::COLLISION_DETECTION_TYPE_CONTINUOUS);
	collision->setSelfCollisionDetectionType(SurgSim::Collision::COLLISION_DETECTION_TYPE_CONTINUOUS);
	physics->setCollisionRepresentation(collision);
	element->addComponent(collision);

	auto copier = std::make_shared<SurgSim::Blocks::TransferPhysicsToVerticesBehavior>("Copier");
	copier->setSource(physics);
	copier->setTarget(gfx);
	element->addComponent(copier);

	return element;
}

std::shared_ptr<SurgSim::Framework::SceneElement> makeRigid(const std::string& filename)
{
	SurgSim::Math::RigidTransform3d pose = SurgSim::Math::makeRigidTranslation(SurgSim::Math::Vector3d(0.0, -0.1, 0.0));

	auto element = std::make_shared<SurgSim::Framework::BasicSceneElement>("RigidMesh");
	element->setPose(pose);
	auto shape = std::make_shared<SurgSim::Math::MeshShape>();
	shape->load(filename);

	auto rigid = std::make_shared<SurgSim::Physics::RigidRepresentation>("Physics");
	rigid->setIsGravityEnabled(false);
	// http://www.engineeringtoolbox.com/wood-density-d_40.html
	rigid->setDensity(5800.0); // Cedar of Lebanon wood density 5800.0 Kg/m-3
	rigid->setShape(shape);
	element->addComponent(rigid);

	auto collision = std::make_shared<SurgSim::Physics::RigidCollisionRepresentation>("Collision");
	collision->setCollisionDetectionType(SurgSim::Collision::COLLISION_DETECTION_TYPE_CONTINUOUS);
	rigid->setCollisionRepresentation(collision);
	collision->setShape(shape);
	element->addComponent(collision);

	auto osgRepresentation = std::make_shared<SurgSim::Graphics::OsgMeshRepresentation>("Graphics");
	osgRepresentation->setShape(shape);
	element->addComponent(osgRepresentation);

	return element;
}
}

class CcdSutureTest : public SurgSim::Physics::RenderTests
{
public:
	void SetUp() override
	{
		SurgSim::Physics::RenderTests::SetUp();

		SurgSim::Framework::Logger::getLoggerManager()->setThreshold(SurgSim::Framework::LOG_LEVEL_DEBUG);
		physicsManager->setRate(150.0);
	}
};

TEST_F(CcdSutureTest, SutureVsMeshedCylinder)
{
	SurgSim::Math::Vector3d cameraPosition(0.25, 0.0, 0.1);
	SurgSim::Math::Vector3d cameraLookAt(0.0, -0.1, 0.0);
	double miliseconds = 25000.0;

	physicsManager->setRate(4000.0);
	physicsManager->setComputations(SurgSim::Physics::createCcdPipeline());

	scene->addSceneElement(std::make_shared<SurgSim::Blocks::VisualizeConstraints>());

	// Load the deformable suture
	scene->addSceneElement(makeSuture("prolene 3.0-fixedExtremity.ply"));
	scene->addSceneElement(makeRigid("cylinder.ply"));

	runTest(cameraPosition, cameraLookAt, miliseconds);
}

TEST_F(CcdSutureTest, Fem1DHalfKnot)
{
	SurgSim::Framework::Logger::getLogger("SegmentSelfContact")->setThreshold(SurgSim::Framework::LOG_LEVEL_INFO);
	scene->addSceneElement(makeSuture("half_knot.ply"));
	physicsManager->setRate(4000.0);
	physicsManager->setComputations(SurgSim::Physics::createCcdPipeline());

	SurgSim::Framework::Logger::getLogger("Collision/SegmentSelfContact")->setThreshold(
		SurgSim::Framework::LOG_LEVEL_WARNING);
	scene->addSceneElement(std::make_shared<SurgSim::Blocks::VisualizeConstraints>());

	SurgSim::Math::Vector3d cameraPosition(0.25, 0.0, 0.25);
	SurgSim::Math::Vector3d cameraLookAt(0.0, 0.0, 0.0);
	double miliseconds = 10000.0;

	runTest(cameraPosition, cameraLookAt, miliseconds);
}

TEST_F(CcdSutureTest, Fem1DLoop)
{
	SurgSim::Framework::Logger::getLogger("SegmentSelfContact")->setThreshold(SurgSim::Framework::LOG_LEVEL_INFO);
	scene->addSceneElement(makeSuture("loop.ply"));
	physicsManager->setRate(1000.0);
	physicsManager->setComputations(SurgSim::Physics::createCcdPipeline());

	SurgSim::Framework::Logger::getLogger("Collision/SegmentSelfContact")->setThreshold(
		SurgSim::Framework::LOG_LEVEL_WARNING);
	scene->addSceneElement(std::make_shared<SurgSim::Blocks::VisualizeConstraints>());

	SurgSim::Math::Vector3d cameraPosition(0.25, 0.0, 0.25);
	SurgSim::Math::Vector3d cameraLookAt(0.0, 0.0, 0.0);
	double miliseconds = 5000.0;

	runTest(cameraPosition, cameraLookAt, miliseconds);
}<commit_msg>RenderTestCcdSuture, run render tests closer to real-time.<commit_after>// This file is a part of the OpenSurgSim project.
// Copyright 2016, SimQuest Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

///\file RenderTestCcdSuture.cpp render test for ccd with Fem1D

#include <memory>

#include "SurgSim/Blocks/TransferPhysicsToVerticesBehavior.h"
#include "SurgSim/Blocks/VisualizeConstraints.h"
#include "SurgSim/Collision/Representation.h"
#include "SurgSim/Framework/BasicSceneElement.h"
#include "SurgSim/Framework/Log.h"
#include "SurgSim/Framework/SceneElement.h"
#include "SurgSim/Graphics/OsgCurveRepresentation.h"
#include "SurgSim/Graphics/OsgMeshRepresentation.h"
#include "SurgSim/Math/LinearSparseSolveAndInverse.h"
#include "SurgSim/Math/MeshShape.h"
#include "SurgSim/Math/OdeSolver.h"
#include "SurgSim/Math/RigidTransform.h"
#include "SurgSim/Math/SegmentMeshShape.h"
#include "SurgSim/Math/Vector.h"
#include "SurgSim/Physics/DeformableCollisionRepresentation.h"
#include "SurgSim/Physics/Fem1DElementBeam.h"
#include "SurgSim/Physics/Fem1DRepresentation.h"
#include "SurgSim/Physics/PhysicsManager.h"
#include "SurgSim/Physics/RenderTests/RenderTest.h"
#include "SurgSim/Physics/RigidCollisionRepresentation.h"
#include "SurgSim/Physics/RigidRepresentation.h"


namespace
{
std::shared_ptr<SurgSim::Framework::SceneElement> makeSuture(const std::string& filename)
{
	auto element = std::make_shared<SurgSim::Framework::BasicSceneElement>("Suture");

	// Physics
	auto physics = std::make_shared<SurgSim::Physics::Fem1DRepresentation>("Physics");
	physics->setFemElementType("SurgSim::Physics::Fem1DElementBeam");
	physics->setLocalPose(SurgSim::Math::RigidTransform3d::Identity());
	physics->loadFem(filename);
	physics->setIntegrationScheme(SurgSim::Math::INTEGRATIONSCHEME_EULER_IMPLICIT);
	physics->setLinearSolver(SurgSim::Math::LINEARSOLVER_LU);
	physics->setRayleighDampingMass(5.0);
	physics->setRayleighDampingStiffness(0.001);
	physics->setIsGravityEnabled(true);
	element->addComponent(physics);

	// Graphics
	auto gfx = std::make_shared<SurgSim::Graphics::OsgCurveRepresentation>("Graphics");
	gfx->setColor(SurgSim::Math::Vector4d(0.0, 0.0, 1.0, 1.0));
	gfx->setAntiAliasing(true);
	gfx->setWidth(0.9);
	element->addComponent(gfx);

	auto collision = std::make_shared<SurgSim::Physics::DeformableCollisionRepresentation>("Collision");
	auto shape = std::make_shared<SurgSim::Math::SegmentMeshShape>();
	shape->load(filename);
	shape->setRadius(0.0001);
	collision->setShape(shape);
	collision->setCollisionDetectionType(SurgSim::Collision::COLLISION_DETECTION_TYPE_CONTINUOUS);
	collision->setSelfCollisionDetectionType(SurgSim::Collision::COLLISION_DETECTION_TYPE_CONTINUOUS);
	physics->setCollisionRepresentation(collision);
	element->addComponent(collision);

	auto copier = std::make_shared<SurgSim::Blocks::TransferPhysicsToVerticesBehavior>("Copier");
	copier->setSource(physics);
	copier->setTarget(gfx);
	element->addComponent(copier);

	return element;
}

std::shared_ptr<SurgSim::Framework::SceneElement> makeRigid(const std::string& filename)
{
	SurgSim::Math::RigidTransform3d pose = SurgSim::Math::makeRigidTranslation(SurgSim::Math::Vector3d(0.0, -0.1, 0.0));

	auto element = std::make_shared<SurgSim::Framework::BasicSceneElement>("RigidMesh");
	element->setPose(pose);
	auto shape = std::make_shared<SurgSim::Math::MeshShape>();
	shape->load(filename);

	auto rigid = std::make_shared<SurgSim::Physics::RigidRepresentation>("Physics");
	rigid->setIsGravityEnabled(false);
	// http://www.engineeringtoolbox.com/wood-density-d_40.html
	rigid->setDensity(5800.0); // Cedar of Lebanon wood density 5800.0 Kg/m-3
	rigid->setShape(shape);
	element->addComponent(rigid);

	auto collision = std::make_shared<SurgSim::Physics::RigidCollisionRepresentation>("Collision");
	collision->setCollisionDetectionType(SurgSim::Collision::COLLISION_DETECTION_TYPE_CONTINUOUS);
	rigid->setCollisionRepresentation(collision);
	collision->setShape(shape);
	element->addComponent(collision);

	auto osgRepresentation = std::make_shared<SurgSim::Graphics::OsgMeshRepresentation>("Graphics");
	osgRepresentation->setShape(shape);
	element->addComponent(osgRepresentation);

	return element;
}
}

class CcdSutureTest : public SurgSim::Physics::RenderTests
{
public:
	void SetUp() override
	{
		SurgSim::Physics::RenderTests::SetUp();

		SurgSim::Framework::Logger::getLoggerManager()->setThreshold(SurgSim::Framework::LOG_LEVEL_DEBUG);
		physicsManager->setComputations(SurgSim::Physics::createCcdPipeline());
		physicsManager->setRate(150.0);
	}
};

TEST_F(CcdSutureTest, SutureVsMeshedCylinder)
{
	scene->addSceneElement(makeSuture("prolene 3.0-fixedExtremity.ply"));
	scene->addSceneElement(makeRigid("cylinder.ply"));
	scene->addSceneElement(std::make_shared<SurgSim::Blocks::VisualizeConstraints>());

	SurgSim::Math::Vector3d cameraPosition(0.25, 0.0, 0.1);
	SurgSim::Math::Vector3d cameraLookAt(0.0, -0.1, 0.0);
	double milliseconds = 5000.0;
	runTest(cameraPosition, cameraLookAt, milliseconds);
}

TEST_F(CcdSutureTest, Fem1DHalfKnot)
{
	scene->addSceneElement(makeSuture("half_knot.ply"));
	scene->addSceneElement(std::make_shared<SurgSim::Blocks::VisualizeConstraints>());

	SurgSim::Math::Vector3d cameraPosition(1.0, 0.0, 1.0);
	SurgSim::Math::Vector3d cameraLookAt(0.0, 0.0, 0.0);
	double milliseconds = 5000.0;
	runTest(cameraPosition, cameraLookAt, milliseconds);
}

TEST_F(CcdSutureTest, Fem1DLoop)
{
	scene->addSceneElement(makeSuture("loop.ply"));
	scene->addSceneElement(std::make_shared<SurgSim::Blocks::VisualizeConstraints>());

	SurgSim::Math::Vector3d cameraPosition(0.25, 0.0, 0.25);
	SurgSim::Math::Vector3d cameraLookAt(0.0, 0.0, 0.0);
	double milliseconds = 5000.0;
	runTest(cameraPosition, cameraLookAt, milliseconds);
}
{'fraction_non_alphanumeric': 0.14490685096153846, 'fraction_numerical': 0.020207331730769232, 'mean_word_length': 8.277351916376306, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 136, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7341', 'n_tokens_mistral': 4755, 'n_tokens_neox': 4589, 'n_words': 645}