17 #ifndef VSR_CHAIN_H_INCLUDED
18 #define VSR_CHAIN_H_INCLUDED
21 #include "vsr_twist.h"
25 namespace vsr{
namespace cga {
38 REVOLUTE, PRISMATIC, CYLINDRICAL, HELICAL, PLANAR, SPHERICAL
48 virtual Frame operator()()
const = 0;
63 Frame operator() (VSR_PRECISION amt) {
return mFrame.moveZ(amt); }
69 Frame operator() (VSR_PRECISION amt) { mAmt = amt;
return rotXY( amt ); }
70 virtual Frame operator()()
const {
return rotXY( mAmt ); }
76 Frame operator() (VSR_PRECISION slide, VSR_PRECISION rotate) {
return mFrame.moveZ(slide).
rotXY( rotate ); }
81 Helical( VSR_PRECISION period = PI, VSR_PRECISION pitch = 1.0 ) : mFrame(), mPeriod( period), mPitch(pitch) {}
82 Frame operator() (VSR_PRECISION amt) {
return Frame(
Gen::mot( Twist::Along( mFrame.
dlz(), mPeriod, mPitch ) * amt ) * mFrame.
mot() ); }
84 VSR_PRECISION mPeriod, mPitch;
89 Frame operator() (VSR_PRECISION dx, VSR_PRECISION dy, VSR_PRECISION rotate) {
Frame f(mFrame);
return f.
move(dx,dy,0).
rotXY( rotate ); }
95 VSR_PRECISION theta, phi;
96 Frame operator() (VSR_PRECISION rx, VSR_PRECISION ry) { theta = rx; phi = ry;
return Frame(
pos(),
Gen::rot(theta,phi) ); }
123 for (
int i = 0; i < mNum; ++i){
136 for (
auto& i : mJoint) i.
reset();
140 for (
auto& i : mJoint) i.
reset();
141 for (
auto& i : mLink) i.
reset();
146 Chain(
const string& s) {
153 Chain(
int n = 3) : mNum(n) {
167 void alloc(
const string& s){
175 mJoint = vector<Frame>(mNum);
176 mLink = vector<Frame>(mNum);
177 mFrame = vector<Frame>(mNum);
180 for (
int i = 0; i < mNum; ++i){
182 if( strncmp( &s[i],
"R", 1 ) == 0) mJoint.push_back( Revolute() );
183 else if (strncmp( &s[i],
"S", 1 ) == 0 ) mJoint.push_back( Spherical() );
215 mFrame = vector<Frame>(n);
216 mLink = vector<Frame>(n);
217 mJoint = vector<Frame>(n);
234 int num()
const {
return mNum; }
243 vector<Frame>& links() {
return mLink; }
244 vector<Frame>& joints() {
return mJoint; }
262 auto rj = Op::rj(
link(k).
vec(), Biv::xy );
263 return mFrame[k].dxy().translate(rj);
268 auto rj = Op::rj(
link(k-1).
vec(), Biv::xy );
269 auto uxy = mFrame[k].dxy().spin( !
link(k-1).
rot() );
270 return uxy.translate(-rj);
297 return Round:: null( Interp::linear<Vec>( mFrame[idx].
vec(), mFrame[idx+1].
vec(), t) );
301 Frame& first() {
return mFrame[0]; }
302 Frame& last() {
return mFrame[mNum -1]; }
310 return Dlp(0,1,0,p[1]);
316 Dll linf(
int k) {
return Op::dl( mFrame[k].
pos() ^ mFrame[k+1].
pos() ^
Inf(1) ).runit() ; }
318 Dll linb(
int k ) {
return Op::dl( mFrame[k].
pos() ^ mFrame[k-1].
pos() ^
Inf(1) ).runit() ; }
320 Dll lin(
const Pnt& p ) {
return Op::dl( mFrame[mNum-1].
pos() ^ p ^
Inf(1) ).runit() ; }
324 if (k==0)
return mJoint[0].mot();
326 return mLink[k-1].mot() * mJoint[k].mot();
330 Mot mot = mJoint[0].mot();
331 mFrame[0].mot( mBaseFrame.
mot() *
mot );
336 Motor mot = mJoint[0].mot();
337 mFrame[0].mot( mBaseFrame.
mot() *
mot );
338 for (
int i = 1; i < mNum; ++i){
339 Mot rel = mLink[i-1].mot() * mJoint[i].mot();
340 mFrame[i].mot( mFrame[i-1].
mot() * rel );
347 Mot mot = mJoint[0].mot();
348 mFrame[0].mot( mBaseFrame.
mot() *
mot );
350 for (
int i = 1; i <= end; ++i){
351 Mot m = mFrame[i-1].mot() * mLink[i-1].mot() * mJoint[i].mot();
358 void fk(
int begin,
int end){
360 for (
int i = begin; i < end; ++i){
361 Mot m = mFrame[i-1].mot() * mLink[i-1].mot() * mJoint[i].mot();
368 void ik(
int end,
int begin){
423 void fabrik(
const Pnt& p,
int end,
int begin,
double err = .01){
426 Sca s = mFrame[end].pos() <= p * -2.0;
431 Pnt base = mFrame[begin].
pos();
446 for (
int i = end; i > begin; --i){
447 mFrame[i].pos( tmpGoal );
450 par = (dll ^ dls).dual();
455 for (
int i = begin; i < end; ++i){
458 par = (dll ^ dls).dual();
460 mFrame[i+1].pos(tmpBase);
463 s = mFrame[ end ].pos() <= p * -2.0;
465 n++;
if (n > 20) {
break; }
535 for (
int i = start; i < (bLoop ? mNum : mNum-1); ++i){
537 int next = i < (mNum-1) ? i+1 : 0;
541 auto target = ( mFrame[next].vec() - mFrame[i].vec() ).unit();
545 target = target.spin( !linkRot * !ry );
547 auto adjustedRot =
Gen::ratio( Vec::y, target );
549 mJoint[i].rot() = adjustedRot;
551 ry = ry * mJoint[i].
rot() * mLink[i].
rot();
561 for (
int i = 0; i < mNum; ++i){
562 int next = (i<mNum-1)?i+1:0;
565 auto yratio =
Gen::ratio( mFrame[i].
y(), mFrame[next].
y() );
568 mLink[i].rot() = !mFrame[i].rot() * !yratio * mFrame[next].rot();
570 auto dv = mFrame[next].vec() - mFrame[i].vec();
574 mLink[i].pos() = dv.spin( !mFrame[i].
rot() ).null();
578 for (
int i = 0; i < mNum; ++i){
579 int next = (i<mNum-1)?i+1:0;
581 auto theta = acos( (mFrame[i].
z()<=mFrame[next].
z())[0] );
582 mLink[i].rot() =
Gen::rotor( Biv::xz * theta * .5 );
583 auto vec = (mFrame[next].vec() - mFrame[i].vec()).
spin( !mFrame[i].
rot() );
static std::vector< Point > split(const Pair &pp)
Split Points from Point Pair.
Dll linf(int k)
Dual Line Forward: Line from kth frame to kth+1 joint.
Definition: vsr_chain.h:316
Mot rel(int k)
relative transformation (lagrangian) at kth joint
Definition: vsr_chain.h:323
static DualSphere dls(VSR_PRECISION r, VSR_PRECISION x, VSR_PRECISION y, VSR_PRECISION z)
Dual Sphere from Coordinate Center (shorthand)
Definition: vsr_cga3D_round.h:79
Definition: vsr_chain.h:93
Dlp xz(const Pnt &p)
Horiz xz Plane Containing Target Point v.
Definition: vsr_chain.h:309
void fk(int begin, int end)
Forward Kinematics: calculate forward from "begin" to "end" joint.
Definition: vsr_chain.h:358
void calcJoints(int start=0, bool bLoop=false)
Derive Joint Rotations from Current Positions.
Definition: vsr_chain.h:531
Frame & link(int k)
set k's Link To Next Joint
Definition: vsr_chain.h:235
NDlp< 5 > Dlp
DualPlane
Definition: vsr_cga3D_types.h:81
void angle(int k, double theta)
Satisfy Specific Angle Constraint at frame k.
Definition: vsr_chain.h:591
Point pos() const
Get Position.
Definition: vsr_cga3D_frame.h:138
Mot mot() const
Get Absolute Motor Relative to Origin.
Circle nextCircle(int k) const
Dual Circle Centered at Joint K Going Through Joint K+1 (in plane of rotation)
Definition: vsr_chain.h:274
void fabrik(const Pnt &p, int end, int begin, double err=.01)
"FABRIK" Iterative Solver [see paper "Inverse Kinematic Solutions using Conformal Geometric Algebra"...
Definition: vsr_chain.h:423
Definition: vsr_chain.h:35
Generic Geometric Number Types (templated on an algebra and a basis )
Definition: vsr_algebra.h:69
Frame & spin()
Spin Step (Local Rotation) */.
Rotor mRot
Orientation.
Definition: vsr_cga3D_frame.h:53
static Rot rot(const Biv &b)
vsr::cga::Rotor from vsr::cga::Bivector
Multivector< algebra, typename algebra::vector_basis > null() const
Conformal Mapping \(\boldsymbol(x)\to n_o + \boldsymbol{x} + \boldsymbol{x}^2n_\infty \) ...
Definition: vsr_generic_op.h:1118
static Mot mot(const Dll &dll)
Generate a vsr::cga::Motor from a vsr::cga::DualLine Axis.
Circle prevCircle(int k) const
Dual Circle Centered at Joint K Going Through Joint K-1 (in plane of rotation)
Definition: vsr_chain.h:279
Definition: vsr_chain.h:87
Frame & reset()
Reset to Origin.
Definition: vsr_cga3D_frame.h:85
Dlp prevPlane(int k) const
Dual Plane of rotation of k-1th joint (translated by link rejection from yz)
Definition: vsr_chain.h:267
vector< Frame > mFrame
Absolute frames of Joints = prevFrame * prevLink * joint.
Definition: vsr_chain.h:118
Dls goalSphere(const Pnt &p, int k)
Sphere at Point p through Joint K.
Definition: vsr_chain.h:285
void calcLinks(bool bOrientation=false)
Derive New Relative Link Frames from current Positions @ param bOrientation: whether to consider curr...
Definition: vsr_chain.h:558
Dlp nextPlane(int k) const
Dual Plane of rotation of kth joint (translated by link rejection from yz)
Definition: vsr_chain.h:261
Vec vec() const
Get Euclidean Vector of position.
Definition: vsr_cga3D_frame.h:142
Dls nextSphere(int k) const
Sphere Centered at Joint K Going Through Joint K+1.
Definition: vsr_chain.h:252
void fk()
Forward Kinematics: Absolute Concatenations of previous frame, previous link, and current joint...
Definition: vsr_chain.h:335
Frame()
Default Constructor.
Frame & frame(int k)
set Absolute Displacement Motor
Definition: vsr_chain.h:237
void fk(int end)
Forward Kinematics: calculate forward to "end" joint.
Definition: vsr_chain.h:345
NInf< 5 > Inf
Infinity
Definition: vsr_cga3D_types.h:68
Definition: vsr_chain.h:80
Frame & mot(const Mot &m)
Set position and orientation by motor (absolute)
Dll dll() const
DualLine Representation of Pose.
Dlp xy(const Pnt &p)
Vert xy Plane Containing Root Target Point v ( NORMALIZED )
Definition: vsr_chain.h:305
Frame link(int k) const
Get k's Link To Next joint.
Definition: vsr_chain.h:239
A sequence of spatial Frames.
Definition: vsr_chain.h:107
vector< Frame > mLink
Relative Link to NEXT joint.
Definition: vsr_chain.h:116
Dls prevSphere(int k) const
Sphere Centered at Joint K Going Through Joint K-1.
Definition: vsr_chain.h:256
Frame joint(int k) const
Get kth Joint's In Socket Transformation.
Definition: vsr_chain.h:240
Dll lin(const Pnt &p)
Dual Line From Kth Joint to Input Target (Default is From Last joint)
Definition: vsr_chain.h:320
static Rot ratio(const Vec &v, const Vec &v2)
vsr::cga::Rotor that takes one vec to another
Definition: vsr_chain.h:67
Pnt at(int idx, double t=0.0)
Pnt at position t along Link idx.
Definition: vsr_chain.h:296
Frame & move()
Translation Step (translate by velocity vector)
the versor library namespace
Definition: vsr_algebra.h:29
Multivector rot(const MultivectorB< B > &b) const
Rotor rot() const
Get 4x4 Rotation Matrix.
Definition: vsr_cga3D_frame.h:97
Frame rotXY(VSR_PRECISION amt) const
Rotate around local xy and return a new frame.
Orthonormal Frame composed from a Position and Orientation.
Definition: vsr_cga3D_frame.h:47
NBiv< 5 > Biv
Bivector
Definition: vsr_cga3D_types.h:63
Point mPos
Position.
Definition: vsr_cga3D_frame.h:52
Frame & joint(int k)
set kth joint's In Socket Transformation
Definition: vsr_chain.h:236
Frame & operator[](int k)
Set kth Absolute Frame.
Definition: vsr_chain.h:246
Definition: vsr_chain.h:61
Frame mBaseFrame
default zero, to tie chains together, set this to another chain's frame.
Definition: vsr_chain.h:111
static Rot rotor(const Biv &b)
vsr::cga::Rotor from vsr::cga::Bivector
Frame frame(int k) const
Get Absolute Displacement Motor.
Definition: vsr_chain.h:241
vector< Frame > mJoint
In Socket Transformation (RDHC, etc) SET THIS directly using joint(i) (all others follow after callin...
Definition: vsr_chain.h:113
Dll dlz() const
z direction dual line
Dll linb(int k)
Dual Line Backward: Line from kth frame to kth-1 joint.
Definition: vsr_chain.h:318
static Point null(const Vec &v)
Null Point from a vec.
Dls lastSphere(const Pnt &p)
Sphere at point p through last link (default, or set arbitary link)
Definition: vsr_chain.h:289
Definition: vsr_chain.h:74
Dll linkf(int k)
Dual Line Forward: Line from kth frame to kth Link.
Definition: vsr_chain.h:314