Skip to content

Commit

Permalink
add vec2/3/4 .dot() .cross()
Browse files Browse the repository at this point in the history
  • Loading branch information
gewang committed Nov 14, 2024
1 parent 9808053 commit c401477
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 4 deletions.
15 changes: 14 additions & 1 deletion VERSIONS
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
ChucK VERSIONS log
------------------

1.5.4.2
1.5.4.2 (November 2024)
=======
(updated) --chugin-path:<path> no longer auto-loads chugins found in the
specified paths; instead the specified paths are added to the import
Expand All @@ -18,6 +18,19 @@ ChucK VERSIONS log
various scenarios.
(fixed) long-standing SndBuf issue, especially prevalent on macOS:
"System error : Too many open files." this was due a combination of
(fixed) vec2/vec3/vec4 implicit cast logic for +=> and -=> operations
(previously, this would cause a crash in some cases)
(added) vector dot product methods for vec2, vec3, and vec4
float vec2.dot( vec2 rhs )
float vec3.dot( vec3 rhs )
float vec4.dot( vec4 rhs )
(added) vector cross product method for vec3 and vec4 (as 3D shorthand)
NOTE: this is the same as vec3*vec3 or vec4*vec4
NOTE: 4D cross product is mathematically undefined; vec4 cross product
ignores the `w` components and computes a 3D cross product;
`w` is also set to zero in the resulting vec4
vec3 vec3.cross( vec3 rhs )
vec4 vec4.cross( vec4 rhs )
(added) --auto-load-chugin-path:<path> flag -- this behaves as
--chugin-path previously did: chugins in the specified path(s) are
auto-loaded; .ck files can be @imported
Expand Down
7 changes: 7 additions & 0 deletions examples/vector/vec3.ck
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ a + b => vec3 sum;
a - b => vec3 diff;
// cross product
a * b => vec3 c;
// another way to do cross product (function)
a.cross(b) => vec3 cross;
// dot product (function)
a.dot(b) => float dot;

// print sum!
<<< "sum:", sum >>>;
// print difference
<<< "diff:", diff >>>;
// print cross product
<<< "cross product:", c >>>;
// print dot product
<<< "dot product:", dot >>>;

// array
[ a, b, @(-1,1,0) ] @=> vec3 group[];
Expand All @@ -37,3 +43,4 @@ v.normalize();
5 * v => vec3 v2;
// print result
<<< "scalar multiply:", v2 >>>;

8 changes: 6 additions & 2 deletions examples/vector/vec4.ck
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ a + b => vec4 sum;
a - b => vec4 diff;
// cross product (ignores w component)
a * b => vec4 c;

// another way to do cross product (ignores w component)
a.cross(b) => vec4 cross;
// dot product (function)
a.dot(b) => float dot;

// print sum!
<<< "sum:", sum >>>;
// print difference
<<< "diff:", diff >>>;
// print cross product
<<< "cross product:", c >>>;

// print dot product
<<< "dot product:", dot >>>;

// array
[ a, b, c ] @=> vec4 group[];
Expand Down
83 changes: 83 additions & 0 deletions src/core/chuck_lang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,11 @@ t_CKBOOL init_class_vec2( Chuck_Env * env, Chuck_Type * type )
func = make_new_mfun( "void", "normalize", vec2_normalize );
if( !type_engine_import_mfun( env, func ) ) goto error;

// add dot() product | 1.5.4.2 (ge & azaday) added
func = make_new_mfun( "float", "dot", vec2_dot );
func->add_arg( "vec2", "rhs" );
if( !type_engine_import_mfun( env, func ) ) goto error;

// end the class import
type_engine_import_class_end( env );

Expand Down Expand Up @@ -747,6 +752,16 @@ t_CKBOOL init_class_vec3( Chuck_Env * env, Chuck_Type * type )
func = make_new_mfun( "void", "normalize", vec3_normalize );
if( !type_engine_import_mfun( env, func ) ) goto error;

// add dot() product | 1.5.4.2 (ge & azaday) added
func = make_new_mfun( "float", "dot", vec3_dot );
func->add_arg( "vec3", "rhs" );
if( !type_engine_import_mfun( env, func ) ) goto error;

// add cross() product | same as vec3 * vec3
func = make_new_mfun( "vec3", "cross", vec3_cross );
func->add_arg( "vec3", "rhs" );
if( !type_engine_import_mfun( env, func ) ) goto error;

// add interp()
func = make_new_mfun( "float", "interp", vec3_interp );
if( !type_engine_import_mfun( env, func ) ) goto error;
Expand Down Expand Up @@ -838,6 +853,17 @@ t_CKBOOL init_class_vec4( Chuck_Env * env, Chuck_Type * type )
func = make_new_mfun( "void", "normalize", vec4_normalize );
if( !type_engine_import_mfun( env, func ) ) goto error;

// add dot() product | 1.5.4.2 (ge & azaday) added
func = make_new_mfun( "float", "dot", vec4_dot );
func->add_arg( "vec4", "rhs" );
if( !type_engine_import_mfun( env, func ) ) goto error;

// add cross() product | same as vec4 * vec4
// (which ignores w and computes a 3d cross product)
func = make_new_mfun( "vec4", "cross", vec4_cross );
func->add_arg( "vec4", "rhs" );
if( !type_engine_import_mfun( env, func ) ) goto error;

// end the class import
type_engine_import_class_end( env );

Expand Down Expand Up @@ -2222,6 +2248,16 @@ CK_DLL_MFUN( vec2_normalize )
}
}

CK_DLL_MFUN( vec2_dot ) // 1.5.4.2 (ge & azaday) added
{
// get lhs value
t_CKVEC2 lhs = *((t_CKVEC2 *)SELF);
// get rhs from argument
t_CKVEC2 rhs = GET_NEXT_VEC2(ARGS);
// compute dot product and set as return value
RETURN->v_float = lhs.x*rhs.x + lhs.y*rhs.y;
}


//-----------------------------------------------------------------------------
// vec3 API
Expand Down Expand Up @@ -2270,6 +2306,28 @@ CK_DLL_MFUN( vec3_normalize )
}
}

CK_DLL_MFUN( vec3_dot ) // 1.5.4.2 (ge & azaday) added
{
// get lhs value
t_CKVEC3 lhs = *((t_CKVEC3 *)SELF);
// get rhs from argument
t_CKVEC3 rhs = GET_NEXT_VEC3(ARGS);
// compute dot product and set as return value
RETURN->v_float = lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z;
}

CK_DLL_MFUN( vec3_cross ) // 1.5.4.2 (ge & azaday) added
{
// get lhs value
t_CKVEC3 lhs = *((t_CKVEC3 *)SELF);
// get rhs from argument
t_CKVEC3 rhs = GET_NEXT_VEC3(ARGS);
// compute cross product and set as return value
RETURN->v_vec3.x = lhs.y*rhs.z - lhs.z*rhs.y;
RETURN->v_vec3.y = lhs.z*rhs.x - lhs.x*rhs.z;
RETURN->v_vec3.z = lhs.x*rhs.y - lhs.y*rhs.x;
}

CK_DLL_MFUN( vec3_interp )
{
// get data pointer
Expand Down Expand Up @@ -2406,6 +2464,31 @@ CK_DLL_MFUN( vec4_normalize )
}
}

CK_DLL_MFUN( vec4_dot ) // 1.5.4.2 (ge & azaday) added
{
// get lhs value
t_CKVEC4 lhs = *((t_CKVEC4 *)SELF);
// get rhs from argument
t_CKVEC4 rhs = GET_NEXT_VEC4(ARGS);
// compute dot product and set as return value
RETURN->v_float = lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z + lhs.w*rhs.w;
}

CK_DLL_MFUN( vec4_cross ) // 1.5.4.2 (ge & azaday) added
{
// get lhs value
t_CKVEC3 lhs = *((t_CKVEC3 *)SELF);
// get rhs from argument
t_CKVEC3 rhs = GET_NEXT_VEC3(ARGS);
// compute cross product and set as return value
// 4D cross product is undefined; as a shorthand we do a 3D cross product
// w is ignored in the calcluation, and the resulting w component is set to 0
RETURN->v_vec4.x = lhs.y*rhs.z - lhs.z*rhs.y;
RETURN->v_vec4.y = lhs.z*rhs.x - lhs.x*rhs.z;
RETURN->v_vec4.z = lhs.x*rhs.y - lhs.y*rhs.x;
RETURN->v_vec4.w = 0;
}




Expand Down
5 changes: 5 additions & 0 deletions src/core/chuck_lang.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,13 @@ CK_DLL_MFUN( vec2_set );
CK_DLL_MFUN( vec2_setAll );
CK_DLL_MFUN( vec2_magnitude );
CK_DLL_MFUN( vec2_normalize );
CK_DLL_MFUN( vec2_dot );
CK_DLL_MFUN( vec3_set );
CK_DLL_MFUN( vec3_setAll );
CK_DLL_MFUN( vec3_magnitude );
CK_DLL_MFUN( vec3_normalize );
CK_DLL_MFUN( vec3_dot );
CK_DLL_MFUN( vec3_cross );
CK_DLL_MFUN( vec3_interp );
CK_DLL_MFUN( vec3_interp_delta_float );
CK_DLL_MFUN( vec3_interp_delta_dur );
Expand All @@ -278,6 +281,8 @@ CK_DLL_MFUN( vec4_set );
CK_DLL_MFUN( vec4_setAll );
CK_DLL_MFUN( vec4_magnitude );
CK_DLL_MFUN( vec4_normalize );
CK_DLL_MFUN( vec4_dot );
CK_DLL_MFUN( vec4_cross );


//-----------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/core/chuck_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4669,7 +4669,7 @@ t_CKTYPE type_engine_check_exp_func_call( Chuck_Env * env, a_Exp exp_func, a_Exp
}
else if( exp_func->s_type == ae_exp_dot_member )
{
EM_error2( exp_func->where,
EM_error2( exp_func->dot_member.where,
"argument type(s) do not match...\n...for function '%s(...)'...",
type_engine_print_exp_dot_member( env, &exp_func->dot_member ).c_str() );
}
Expand Down
51 changes: 51 additions & 0 deletions src/test/01-Basic/257-vec3.ck
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// test vec3
// including .cross() and .dot(), which were added in 1.5.4.2
// vec3 is a primitive type

@(1,0,0) => vec3 a;
// declare another
@(0,1,0) => vec3 b;

// add them
a + b => vec3 sum;
// difference
a - b => vec3 diff;
// cross product
a * b => vec3 c;
// another way to do cross product (function)
a.cross(b) => vec3 cross;
// dot product (function)
a.dot(b) => float dot;

// print sum!
<<< "sum:", sum >>>;
// print difference
<<< "diff:", diff >>>;
// print cross product
<<< "cross product:", c >>>;
<<< "cross product (function):", cross >>>;
// print dot product
<<< "dot product:", dot >>>;

// array
[ a, b, @(-1,1,0) ] @=> vec3 group[];
// print them
<<< group[0], group[1], group[2] >>>;

// another v
vec3 v;
// set value
v.set( 2,2,2 );
// print magnitude
<<< "magnitude:", v.magnitude() >>>;
// normalize
v.normalize();
// print vector
<<< "normalize:", v >>>;

// multiply
5 * v => vec3 v2;
// print result
<<< "scalar multiply:", v2 >>>;


9 changes: 9 additions & 0 deletions src/test/01-Basic/257-vec3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sum: #(1.0000,1.0000,0.0000)
diff: #(1.0000,-1.0000,0.0000)
cross product: #(0.0000,0.0000,1.0000)
cross product (function): #(0.0000,0.0000,1.0000)
dot product: 0.000000
#(1.0000,0.0000,0.0000) #(0.0000,1.0000,0.0000) #(-1.0000,1.0000,0.0000)
magnitude: 3.464102
normalize: #(0.5774,0.5774,0.5774)
scalar multiply: #(2.8868,2.8868,2.8868)
31 changes: 31 additions & 0 deletions src/test/01-Basic/258-vec4.ck
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// vec4 is a primitive type
@(1,2,3,4) => vec4 a;
// declare another
@(5,6,7,8) => vec4 b;

// add them
a + b => vec4 sum;
// difference
a - b => vec4 diff;
// cross product (ignores w component)
a * b => vec4 c;
// another way to do cross product (ignores w component)
a.cross(b) => vec4 cross;
// dot product (function)
a.dot(b) => float dot;

// print sum!
<<< "sum:", sum >>>;
// print difference
<<< "diff:", diff >>>;
// print cross product
<<< "cross product:", c >>>;
<<< "cross product (function):", cross >>>;
// print dot product
<<< "dot product:", dot >>>;

// array
[ a, b, c ] @=> vec4 group[];

// print them
<<< group[0], group[1], group[2] >>>;
6 changes: 6 additions & 0 deletions src/test/01-Basic/258-vec4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sum: #(6.0000,8.0000,10.0000,12.0000)
diff: #(-4.0000,-4.0000,-4.0000,-4.0000)
cross product: #(-4.0000,8.0000,-4.0000,0.0000)
cross product (function): #(-4.0000,8.0000,-4.0000,0.0000)
dot product: 70.000000
#(1.0000,2.0000,3.0000,4.0000) #(5.0000,6.0000,7.0000,8.0000) #(-4.0000,8.0000,-4.0000,0.0000)

0 comments on commit c401477

Please sign in to comment.