diff --git a/CMakeLists.txt b/CMakeLists.txt index 5fb1346..83ec509 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,7 @@ FetchContent_MakeAvailable(glm) set(METHODS_PATH "./src/*/methods/*.cpp") -set(VERSION "V2") +set(VERSION "V3") file(GLOB_RECURSE METHODS_SRC "${METHODS_PATH}") add_executable(main${VERSION} ./src/testMain.cpp ${METHODS_SRC} ) diff --git a/README.md b/README.md index 11c315f..f489d5f 100644 --- a/README.md +++ b/README.md @@ -22,10 +22,16 @@ Per spostare l'intera scena si tiene premuto il tasto centrale del mouse. - Aggiustato calcolo dei pivot implementando le rotazioni con algebra affine e glm - Aggiuta classe caviglia (per differenziarla dalla coscia) -Per compliare: +## Nella versione v0.3: +- Applicato refactoring delle classi pieces +- Aggiunta pezzo torso +- Nel testMain vengono agganciate caviglia e coscia al nuovo torso + + +# Per compliare: cmake --build -Per lanciare: +# Per lanciare: - ./build/bin/mainV2 + ./build/bin/mainV3 diff --git a/build/.cmake/api/v1/reply/codemodel-v2-dbb57b2013933c842c1b.json b/build/.cmake/api/v1/reply/codemodel-v2-48a6e2c377bb606c3455.json similarity index 99% rename from build/.cmake/api/v1/reply/codemodel-v2-dbb57b2013933c842c1b.json rename to build/.cmake/api/v1/reply/codemodel-v2-48a6e2c377bb606c3455.json index 926ad3b..942b54e 100644 --- a/build/.cmake/api/v1/reply/codemodel-v2-dbb57b2013933c842c1b.json +++ b/build/.cmake/api/v1/reply/codemodel-v2-48a6e2c377bb606c3455.json @@ -573,9 +573,9 @@ }, { "directoryIndex" : 0, - "id" : "mainV2::@6890427a1f51a3e7e1df", - "jsonFile" : "target-mainV2-Debug-13e85d69c1816e9e6763.json", - "name" : "mainV2", + "id" : "mainV3::@6890427a1f51a3e7e1df", + "jsonFile" : "target-mainV3-Debug-ee31761362aa79611c23.json", + "name" : "mainV3", "projectIndex" : 0 }, { diff --git a/build/.cmake/api/v1/reply/index-2026-05-31T08-35-42-0883.json b/build/.cmake/api/v1/reply/index-2026-05-31T08-58-45-0525.json similarity index 94% rename from build/.cmake/api/v1/reply/index-2026-05-31T08-35-42-0883.json rename to build/.cmake/api/v1/reply/index-2026-05-31T08-58-45-0525.json index 83fd289..ea67de3 100644 --- a/build/.cmake/api/v1/reply/index-2026-05-31T08-35-42-0883.json +++ b/build/.cmake/api/v1/reply/index-2026-05-31T08-58-45-0525.json @@ -26,7 +26,7 @@ "objects" : [ { - "jsonFile" : "codemodel-v2-dbb57b2013933c842c1b.json", + "jsonFile" : "codemodel-v2-48a6e2c377bb606c3455.json", "kind" : "codemodel", "version" : { @@ -99,7 +99,7 @@ } }, { - "jsonFile" : "codemodel-v2-dbb57b2013933c842c1b.json", + "jsonFile" : "codemodel-v2-48a6e2c377bb606c3455.json", "kind" : "codemodel", "version" : { diff --git a/build/.cmake/api/v1/reply/target-mainV2-Debug-13e85d69c1816e9e6763.json b/build/.cmake/api/v1/reply/target-mainV3-Debug-ee31761362aa79611c23.json similarity index 93% rename from build/.cmake/api/v1/reply/target-mainV2-Debug-13e85d69c1816e9e6763.json rename to build/.cmake/api/v1/reply/target-mainV3-Debug-ee31761362aa79611c23.json index 70e0259..f4c3d84 100644 --- a/build/.cmake/api/v1/reply/target-mainV2-Debug-13e85d69c1816e9e6763.json +++ b/build/.cmake/api/v1/reply/target-mainV3-Debug-ee31761362aa79611c23.json @@ -2,7 +2,7 @@ "artifacts" : [ { - "path" : "bin/mainV2" + "path" : "bin/mainV3" } ], "backtrace" : 1, @@ -168,7 +168,9 @@ 3, 4, 5, - 6 + 6, + 7, + 8 ] } ], @@ -191,7 +193,7 @@ "id" : "glm::@ce54d98d7b326a3e12a8" } ], - "id" : "mainV2::@6890427a1f51a3e7e1df", + "id" : "mainV3::@6890427a1f51a3e7e1df", "link" : { "commandFragments" : @@ -294,8 +296,8 @@ "id" : "glm::@ce54d98d7b326a3e12a8" } ], - "name" : "mainV2", - "nameOnDisk" : "mainV2", + "name" : "mainV3", + "nameOnDisk" : "mainV3", "paths" : { "build" : ".", @@ -313,7 +315,9 @@ 3, 4, 5, - 6 + 6, + 7, + 8 ] } ], @@ -343,6 +347,12 @@ "path" : "src/joints/methods/rigid_joint_class.cpp", "sourceGroupIndex" : 0 }, + { + "backtrace" : 1, + "compileGroupIndex" : 0, + "path" : "src/pieces/methods/caviglia_class.cpp", + "sourceGroupIndex" : 0 + }, { "backtrace" : 1, "compileGroupIndex" : 0, @@ -355,6 +365,12 @@ "path" : "src/pieces/methods/sensore_class.cpp", "sourceGroupIndex" : 0 }, + { + "backtrace" : 1, + "compileGroupIndex" : 0, + "path" : "src/pieces/methods/torso.cpp", + "sourceGroupIndex" : 0 + }, { "backtrace" : 1, "compileGroupIndex" : 0, diff --git a/build/bin/mainV3 b/build/bin/mainV3 new file mode 100755 index 0000000..022b02d Binary files /dev/null and b/build/bin/mainV3 differ diff --git a/src/include.hpp b/src/include.hpp index dbfb9db..741e112 100644 --- a/src/include.hpp +++ b/src/include.hpp @@ -4,5 +4,6 @@ #include "pieces/headers/coscia.hpp" #include "pieces/headers/caviglia.hpp" #include "pieces/headers/sensore.hpp" +#include "pieces/headers/torso.hpp" #include "joints/headers/rigid_joint.hpp" #include "joints/headers/pivot_joint.hpp" \ No newline at end of file diff --git a/src/pieces/headers/caviglia.hpp b/src/pieces/headers/caviglia.hpp index 237af3f..23c5031 100644 --- a/src/pieces/headers/caviglia.hpp +++ b/src/pieces/headers/caviglia.hpp @@ -3,11 +3,12 @@ #ifndef CAVIGLIA_H #define CAVIGLIA_H -const sf::Vector2f caviglia_Dim = {60, 200}; -const sf::Color caviglia_Col = sf::Color(230,160,11,255); - class Caviglia : public PieceInterface{ + private: + const sf::Vector3f caviglia_Dim = {60, 200, 60}; + const sf::Color caviglia_Col = sf::Color(230,160,11,255); + public: Caviglia(rb::Vector3 coords, _Float16 mass); diff --git a/src/pieces/headers/coscia.hpp b/src/pieces/headers/coscia.hpp index bfbe766..28b44e6 100644 --- a/src/pieces/headers/coscia.hpp +++ b/src/pieces/headers/coscia.hpp @@ -3,11 +3,13 @@ #ifndef COSCIA_H #define COSCIA_H -const sf::Vector2f coscia_Dim = {80, 200}; -const sf::Color coscia_Col = sf::Color::Yellow; class Coscia : public PieceInterface{ + private: + const sf::Vector3f coscia_Dim = {80, 200, 80}; + const sf::Color coscia_Col = sf::Color::Yellow; + public: Coscia(rb::Vector3 coords, _Float16 mass); diff --git a/src/pieces/headers/piece_interface.hpp b/src/pieces/headers/piece_interface.hpp index e7f4fce..7b261e3 100644 --- a/src/pieces/headers/piece_interface.hpp +++ b/src/pieces/headers/piece_interface.hpp @@ -17,11 +17,17 @@ enum class ReferencePlane { class PieceInterface{ protected: - //std::vector joints; + void initialize_shapes(sf::Vector3f dim){ + shapeXZ = new sf::RectangleShape({dim.x, dim.y}); + shapeYZ = new sf::RectangleShape({dim.z, dim.y}); + shapeXZ->setOrigin({dim.x/2,dim.y/2}); + shapeYZ->setOrigin({dim.z/2,dim.y/2}); + shapeXZ->setFillColor(color); + shapeYZ->setFillColor(color); + } public: - sf::Shape* shape; - sf::Vector2f size; + sf::Shape* shapeXZ, *shapeYZ; rb::Vector3 globalPos; rb::rigidbody body; sf::Color color; diff --git a/src/pieces/headers/sensore.hpp b/src/pieces/headers/sensore.hpp index 22d3d12..dc3f518 100644 --- a/src/pieces/headers/sensore.hpp +++ b/src/pieces/headers/sensore.hpp @@ -3,11 +3,13 @@ #ifndef SENSORE_H #define SENSORE_H -const sf::Vector2f sensore_Dim = {30, 60}; -const sf::Color sensore_Col = sf::Color::Red; + class Sensore : public PieceInterface{ private: + const sf::Vector3f sensore_Dim = {30, 60, 30}; + const sf::Color sensore_Col = sf::Color::Red; + std::vector> accData; std::vector> gData; std::vector> rotData; @@ -21,6 +23,7 @@ class Sensore : public PieceInterface{ //funzioni ausiliarie void calcRotWithG(unsigned int index); + public: Sensore(rb::Vector3 coords, _Float16 mass); diff --git a/src/pieces/headers/torso.hpp b/src/pieces/headers/torso.hpp new file mode 100644 index 0000000..d93a387 --- /dev/null +++ b/src/pieces/headers/torso.hpp @@ -0,0 +1,21 @@ +#include "piece_interface.hpp" + +#ifndef TORSO_H +#define TORSO_H + + + + +class Torso : public PieceInterface{ + private: + const sf::Vector3f torso_Dim = {100, 100, 250}; + const sf::Color torso_Col = sf::Color::Red; + public: + Torso(rb::Vector3 coords, _Float16 mass); + ~Torso(); + + void update(sf::Clock cl) override; + sf::Shape* draw(ReferencePlane plane) override; +}; + +#endif \ No newline at end of file diff --git a/src/pieces/methods/caviglia_class.cpp b/src/pieces/methods/caviglia_class.cpp index c2ab853..d3be389 100644 --- a/src/pieces/methods/caviglia_class.cpp +++ b/src/pieces/methods/caviglia_class.cpp @@ -1,17 +1,18 @@ #include "../headers/caviglia.hpp" Caviglia::Caviglia(rb::Vector3 coords, _Float16 mass){ - size = caviglia_Dim; - rb::Vector3 com = {size.x/2,0, size.y/2}; + rb::Vector3 com = {caviglia_Dim.x/2,caviglia_Dim.x/2, caviglia_Dim.y/2}; body = rb::rigidbody(coords, com, mass); color = caviglia_Col; - shape = new sf::RectangleShape(size); - shape->setOrigin({size.x/2,size.y/2}); globalPos = {0,0,0}; + + initialize_shapes(caviglia_Dim); } + Caviglia::~Caviglia(){ - delete shape; + delete shapeXZ; + delete shapeYZ; } void Caviglia::update(sf::Clock cl){ @@ -19,22 +20,31 @@ void Caviglia::update(sf::Clock cl){ } sf::Shape* Caviglia::draw(ReferencePlane plane){ - shape->setFillColor(color); + rb::Vector3 tmpPos = body.getPos(); rb::Vector3_s tmpRot = body.getRot(); switch (plane) { case ReferencePlane::XZ: - shape->setPosition({tmpPos[0]+globalPos[0],tmpPos[2]+globalPos[2]}); + { + sf::Shape* shape = shapeXZ; shape->setRotation(sf::Angle(sf::radians(tmpRot[2]))); + shape->setPosition({tmpPos[0]+globalPos[0],tmpPos[2]+globalPos[2]}); + return shape;} + break; + + case ReferencePlane::YZ: + { + sf::Shape* shape = shapeYZ; + shape->setRotation(sf::Angle(sf::radians(tmpRot[1]))); + shape->setPosition({tmpPos[1]+globalPos[1],tmpPos[2]+globalPos[2]}); + return shape;} break; - default: break; } - - return shape; + return nullptr; } \ No newline at end of file diff --git a/src/pieces/methods/coscia_class.cpp b/src/pieces/methods/coscia_class.cpp index 9c0e409..ce882e1 100644 --- a/src/pieces/methods/coscia_class.cpp +++ b/src/pieces/methods/coscia_class.cpp @@ -1,17 +1,16 @@ #include "../headers/coscia.hpp" Coscia::Coscia(rb::Vector3 coords, _Float16 mass){ - size = coscia_Dim; - rb::Vector3 com = {size.x/2,0, size.y/2}; + rb::Vector3 com = {coscia_Dim.x/2,coscia_Dim.z/2,coscia_Dim.y/2}; body = rb::rigidbody(coords, com, mass); color = coscia_Col; - shape = new sf::RectangleShape(size); - shape->setOrigin({size.x/2,size.y/2}); globalPos = {0,0,0}; + initialize_shapes(coscia_Dim); } Coscia::~Coscia(){ - delete shape; + delete shapeXZ; + delete shapeYZ; } void Coscia::update(sf::Clock cl){ @@ -19,22 +18,33 @@ void Coscia::update(sf::Clock cl){ } sf::Shape* Coscia::draw(ReferencePlane plane){ - shape->setFillColor(color); + rb::Vector3 tmpPos = body.getPos(); rb::Vector3_s tmpRot = body.getRot(); + switch (plane) { case ReferencePlane::XZ: - shape->setPosition({tmpPos[0]+globalPos[0],tmpPos[2]+globalPos[2]}); + { + sf::Shape* shape = shapeXZ; shape->setRotation(sf::Angle(sf::radians(tmpRot[2]))); + shape->setPosition({tmpPos[0]+globalPos[0],tmpPos[2]+globalPos[2]}); + return shape;} + break; + + case ReferencePlane::YZ: + { + sf::Shape* shape = shapeYZ; + shape->setRotation(sf::Angle(sf::radians(tmpRot[1]))); + shape->setPosition({tmpPos[1]+globalPos[1],tmpPos[2]+globalPos[2]}); + return shape;} break; - default: break; } - return shape; + return nullptr; } \ No newline at end of file diff --git a/src/pieces/methods/sensore_class.cpp b/src/pieces/methods/sensore_class.cpp index d9a4850..03611c6 100644 --- a/src/pieces/methods/sensore_class.cpp +++ b/src/pieces/methods/sensore_class.cpp @@ -2,12 +2,11 @@ Sensore::Sensore(rb::Vector3 coords, _Float16 mass){ - size = sensore_Dim; - rb::Vector3 com = {size.x/2,0, size.y/2}; + rb::Vector3 com = {sensore_Dim.x/2,sensore_Dim.z/2, sensore_Dim.y/2}; body = rb::rigidbody(coords, com, mass); color = sensore_Col; - shape = new sf::RectangleShape(size); globalPos = {0,0,0}; + initialize_shapes(sensore_Dim); } Sensore::Sensore(rb::Vector3 coords, _Float16 mass, unsigned int st, unsigned int dataIntvl, std::vector> data) : Sensore(coords, mass){ @@ -18,7 +17,8 @@ Sensore::Sensore(rb::Vector3 coords, _Float16 mass, unsigned int st, unsigned in Sensore::~Sensore(){ - delete shape; + delete shapeXZ; + delete shapeYZ; } void Sensore::initCSV(std::vector> data){ @@ -48,6 +48,8 @@ void Sensore::update(sf::Clock cl){ //calcolo la posizione e velocità calcRotWithG(dataPos); + + body.setAcc(rb::Vector3{accData[dataPos]}); body.step(cl); } @@ -55,30 +57,33 @@ void Sensore::update(sf::Clock cl){ } sf::Shape* Sensore::draw(ReferencePlane plane){ - shape->setFillColor(color); - - shape->setOrigin({sensore_Dim.x/2, sensore_Dim.y/2}); rb::Vector3_s tmpRot = body.getRot(); - - rb::Vector3 tmpPos = body.getPos(); switch (plane) { case ReferencePlane::XZ: + { + sf::Shape* shape = shapeXZ; shape->setRotation(sf::Angle(sf::radians(tmpRot[2]))); shape->setPosition({tmpPos[0]+globalPos[0],tmpPos[2]+globalPos[2]}); + return shape;} + break; + + case ReferencePlane::YZ: + { + sf::Shape* shape = shapeYZ; + shape->setRotation(sf::Angle(sf::radians(tmpRot[1]))); + shape->setPosition({tmpPos[1]+globalPos[1],tmpPos[2]+globalPos[2]}); + return shape;} break; default: break; } - - - - return shape; + return nullptr; } @@ -99,4 +104,10 @@ void Sensore::calcRotWithG(unsigned int index){ // calcolo rotazione con valori body.setRot(rb::Vector3_s{_Float16( tmpAX),_Float16( tmpAY),_Float16( tmpAZ) }); -} \ No newline at end of file +} +/* +void Sensore::calcRotWithConstraint(){ + +};*/ + +/////////////// cinematica inversa \ No newline at end of file diff --git a/src/pieces/methods/torso.cpp b/src/pieces/methods/torso.cpp new file mode 100644 index 0000000..c85f899 --- /dev/null +++ b/src/pieces/methods/torso.cpp @@ -0,0 +1,50 @@ +#include "../headers/torso.hpp" + +Torso::Torso(rb::Vector3 coords, _Float16 mass){ + rb::Vector3 com = {torso_Dim.x/2, torso_Dim.y/2, torso_Dim.z/2}; + body = rb::rigidbody(coords,com, mass); + color = torso_Col; + globalPos = {0,0,0}; + + initialize_shapes(torso_Dim); +} + +Torso::~Torso(){ + delete shapeXZ; + delete shapeYZ; +} + +void Torso::update(sf::Clock cl){ + +} + +sf::Shape* Torso::draw(ReferencePlane plane){ + + rb::Vector3_s tmpRot = body.getRot(); + rb::Vector3 tmpPos = body.getPos(); + + + switch (plane) + { + case ReferencePlane::XZ: + { + sf::Shape* shape = shapeXZ; + shape->setRotation(sf::Angle(sf::radians(tmpRot[2]))); + shape->setPosition({tmpPos[0]+globalPos[0],tmpPos[2]+globalPos[2]}); + return shape;} + break; + + case ReferencePlane::YZ: + { + sf::Shape* shape = shapeYZ; + shape->setRotation(sf::Angle(sf::radians(tmpRot[1]))); + shape->setPosition({tmpPos[1]+globalPos[1],tmpPos[2]+globalPos[2]}); + return shape;} + break; + + default: + break; + } + + return nullptr; +} \ No newline at end of file diff --git a/src/sfml_util.cpp b/src/sfml_util.cpp index 344d2bb..9ce978e 100644 --- a/src/sfml_util.cpp +++ b/src/sfml_util.cpp @@ -22,6 +22,7 @@ struct State sf::Vector2f cameraOffset = {0.,0.}; sf::Clock clock; + ReferencePlane selectedPlane = ReferencePlane::XZ; int selected = -1; @@ -72,6 +73,12 @@ void handle(const sf::Event::TextEntered &textEnter, State &gs) void handle(const sf::Event::KeyPressed &keyPressed, State &gs) { + if (keyPressed.scancode == sf::Keyboard::Scancode::Space){ + if (gs.selectedPlane == ReferencePlane::XZ) + gs.selectedPlane = ReferencePlane::YZ; + else + gs.selectedPlane = ReferencePlane::XZ; + } } void handle(const sf::Event::MouseMoved &mouseMoved, State &gs) @@ -113,7 +120,6 @@ void handle(const sf::Event::MouseButtonPressed &mouseBP, State &gs) if (dist(pos,mouseBP.position) < 20){ gs.selected = i; - break; } i++; } @@ -127,7 +133,6 @@ void handle(const sf::Event::MouseButtonPressed &mouseBP, State &gs) if (dist(pos,mouseBP.position) < 20){ gs.selected = i; - break; } i++; } @@ -173,7 +178,7 @@ void doGraphics(State &gs) doGUI(gs); for(PieceInterface* p: gs.pieces){ - gs.window.draw(*p->draw(ReferencePlane::XZ)); + gs.window.draw(*p->draw(gs.selectedPlane)); } // TODO: add here code to display shapes in your canvas diff --git a/src/testMain.cpp b/src/testMain.cpp index 4a1b0f4..eafa386 100755 --- a/src/testMain.cpp +++ b/src/testMain.cpp @@ -40,7 +40,7 @@ int main() { gs.pieces.push_back(new Coscia (rb::Vector3{300,10,300},2)); gs.pieces.push_back(new Sensore (rb::Vector3{300,300,300},_Float16( 0.2 ),900,3000,coscia)); - gs.pieces.push_back(new Coscia (rb::Vector3{300,10,500},1)); + gs.pieces.push_back(new Caviglia (rb::Vector3{300,10,500},1)); gs.pieces[1]->body.setRot({0,0,0}); @@ -49,6 +49,8 @@ int main() { const auto& caviglia = processor.getData(); gs.pieces.push_back(new Sensore (rb::Vector3{300,700,500},_Float16( 0.2 ),900,3000,caviglia)); + gs.pieces.push_back(new Torso(rb::Vector3{300,400,150},2)); + // modifico la rotazione relativa della gamba gs.pieces[1]->body.setRot({0,0,_Float16 (1.6)}); gs.pieces[3]->body.setRot({0,0,_Float16 (1.7)}); @@ -59,6 +61,8 @@ int main() { gs.joints.push_back(new RigidJoint(gs.pieces[1], {gs.pieces[0]})); gs.joints.push_back(new PivotJoint(gs.pieces[1], {gs.pieces[3]}, rb::Vector3{0,0,100})); gs.joints.push_back(new RigidJoint(gs.pieces[3], {gs.pieces[2]})); + gs.joints.push_back(new PivotJoint(gs.pieces[4], {gs.pieces[1]}, rb::Vector3{0,0,50})); + printf("Ho costruito tutto!\n"); }