Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
electric-field
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
repos.tanguy.cavagna
physics
electric-field
Commits
26d6a4dc
Commit
26d6a4dc
authored
3 years ago
by
tanguy.cavagna
Browse files
Options
Downloads
Patches
Plain Diff
Renamed variable name
parent
c5da2377
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!3
Hotfix : renamed vector variable name to vec2
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
vector/test.c
+96
-0
96 additions, 0 deletions
vector/test.c
vector/vector.c
+10
-10
10 additions, 10 deletions
vector/vector.c
vector/vector.h
+8
-8
8 additions, 8 deletions
vector/vector.h
with
114 additions
and
18 deletions
vector/test.c
0 → 100644
+
96
−
0
View file @
26d6a4dc
// cmocka includes
#include
<stdarg.h>
#include
<stddef.h>
#include
<setjmp.h>
#include
<stdint.h>
#include
<cmocka.h>
// vector include
#include
<stdio.h>
#include
<stdbool.h>
#include
"vector.h"
//* ====================
//* Tests descriptions
//* ====================
static
void
vector_initialization
()
{
vec2
v
=
{
2
,
3
};
assert_non_null
(
&
v
);
}
static
void
vector_addition
()
{
vec2
v1
=
{
2
,
5
};
vec2
v2
=
{
3
,
7
};
vec2
v3
;
vec2
target
=
{
5
,
12
};
v3
=
add_vector
(
v1
,
v2
);
assert_memory_equal
(
&
v3
,
&
target
,
sizeof
(
target
));
}
static
void
vector_substraction
()
{
vec2
v1
=
{
2
,
5
};
vec2
v2
=
{
3
,
7
};
vec2
v3
;
vec2
target
=
{
-
1
,
-
2
};
v3
=
sub_vector
(
v1
,
v2
);
assert_memory_equal
(
&
v3
,
&
target
,
sizeof
(
target
));
}
static
void
vector_multiplication_scalar
()
{
vec2
v1
=
{
2
,
5
};
int
scal
=
2
;
vec2
v2
;
vec2
target
=
{
4
,
10
};
v2
=
mult_by_scalar
(
v1
,
scal
);
assert_memory_equal
(
&
v2
,
&
target
,
sizeof
(
target
));
}
static
void
vector_to_polar_convertion
()
{
vec2
c_v
=
{
84
.
85
,
-
84
.
85
};
polarVector
p_v
;
polarVector
target
=
{
119
.
996
,
-
45
};
p_v
=
to_polar
(
c_v
);
assert_float_equal
(
p_v
.
a
,
target
.
a
,
1e-3
);
assert_float_equal
(
p_v
.
f
,
target
.
f
,
1e-3
);
}
static
void
vector_to_cartesian_convertion
()
{
polarVector
p_v
=
{
200
,
60
};
vec2
c_v
;
vec2
target
=
{
100
,
173
.
205
};
c_v
=
to_cartesian
(
p_v
);
assert_float_equal
(
c_v
.
x
,
target
.
x
,
1e-3
);
assert_float_equal
(
c_v
.
y
,
target
.
y
,
1e-3
);
}
//* ====================
//* Run test
//* ====================
int
main
(
void
)
{
const
struct
CMUnitTest
tests
[]
=
{
cmocka_unit_test
(
vector_initialization
),
cmocka_unit_test
(
vector_addition
),
cmocka_unit_test
(
vector_substraction
),
cmocka_unit_test
(
vector_multiplication_scalar
),
cmocka_unit_test
(
vector_to_polar_convertion
),
cmocka_unit_test
(
vector_to_cartesian_convertion
),
};
return
cmocka_run_group_tests
(
tests
,
NULL
,
NULL
);
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
vector/vector.c
+
10
−
10
View file @
26d6a4dc
...
...
@@ -16,8 +16,8 @@
\param v1 First vector to add
\param v2 Second vector to add
*/
vec
tor
add_vector
(
vec
tor
v1
,
vec
tor
v2
)
{
vec
tor
resultVector
;
vec
2
add_vector
(
vec
2
v1
,
vec
2
v2
)
{
vec
2
resultVector
;
double
finalX
=
v1
.
x
+
v2
.
x
;
double
finalY
=
v1
.
y
+
v2
.
y
;
...
...
@@ -33,7 +33,7 @@ vector add_vector(vector v1, vector v2) {
\param v1 First vector
\param v2 Vector we want to substract to the first
*/
vec
tor
sub_vector
(
vec
tor
v1
,
vec
tor
v2
)
{
vec
2
sub_vector
(
vec
2
v1
,
vec
2
v2
)
{
// First we reverse the direction of the vector we want to substract
v2
.
x
*=
-
1
;
v2
.
y
*=
-
1
;
...
...
@@ -47,8 +47,8 @@ vector sub_vector(vector v1, vector v2) {
\param v1 Vector to multiply
\param scalar Scalar to use for the multiplication
*/
vec
tor
mult_by_scalar
(
vec
tor
v1
,
int
scalar
)
{
vec
tor
resultVector
=
{
v1
.
x
*
scalar
,
v1
.
y
*
scalar
};
vec
2
mult_by_scalar
(
vec
2
v1
,
int
scalar
)
{
vec
2
resultVector
=
{
v1
.
x
*
scalar
,
v1
.
y
*
scalar
};
return
resultVector
;
}
...
...
@@ -57,7 +57,7 @@ vector mult_by_scalar(vector v1, int scalar) {
\brief Get the norm of a given vector
\param v1 Vector to multiply
*/
double
norm
(
vec
tor
v1
)
{
double
norm
(
vec
2
v1
)
{
double
vectorNorm
;
vectorNorm
=
sqrt
(
pow
(
v1
.
x
,
2
)
+
pow
(
v1
.
y
,
2
));
...
...
@@ -70,7 +70,7 @@ double norm(vector v1) {
\param v1 First vector to multiply
\param v2 Second vector to multiply
*/
double
dot_multiply
(
vec
tor
v1
,
vec
tor
v2
)
{
double
dot_multiply
(
vec
2
v1
,
vec
2
v2
)
{
return
(
v1
.
x
*
v2
.
x
)
+
(
v1
.
y
*
v2
.
y
);
}
...
...
@@ -78,7 +78,7 @@ double dot_multiply(vector v1, vector v2) {
\brief Convert a Cartesian vector to a Polar vector
\param v1 Cartesian vector
*/
polarVector
to_polar
(
vec
tor
v1
)
{
polarVector
to_polar
(
vec
2
v1
)
{
polarVector
p_vector
;
p_vector
.
f
=
norm
(
v1
);
...
...
@@ -91,8 +91,8 @@ polarVector to_polar(vector v1) {
\brief Convert a Polar vector to a Cartesian vector
\param v1 Polar vector
*/
vec
tor
to_cartesian
(
polarVector
v1
)
{
vec
tor
c_vector
;
vec
2
to_cartesian
(
polarVector
v1
)
{
vec
2
c_vector
;
// The cos work with radian angle. Convert degree to radiant inside the cos function to have the correct force
c_vector
.
x
=
v1
.
f
*
cos
(
v1
.
a
*
(
M_PI
/
180
.
0
));
...
...
This diff is collapsed.
Click to expand it.
vector/vector.h
+
8
−
8
View file @
26d6a4dc
...
...
@@ -11,7 +11,7 @@
typedef
struct
{
double
x
;
double
y
;
}
vec
tor
;
}
vec
2
;
/**
* @brief Polar vector used for representation only
...
...
@@ -21,13 +21,13 @@ typedef struct {
double
a
;
/* Angle of the vector */
}
polarVector
;
vec
tor
add_vector
(
vec
tor
v1
,
vec
tor
v2
);
/* Add a vector to another and return the result as a vector */
vec
tor
sub_vector
(
vec
tor
v1
,
vec
tor
v2
);
/* Substract a vector to another vector and return the result as a vector */
vec
tor
mult_by_scalar
(
vec
tor
v1
,
int
scalar
);
/* Multiply a vector by a scalar */
double
dot_multiply
(
vec
tor
v1
,
vec
tor
v2
);
/* Multiply two vectors with the dot product formula */
double
norm
(
vec
tor
v1
);
/* Get the norm of a given vector */
polarVector
to_polar
(
vec
tor
v1
);
/* Convert a Cartesian vector to a Polar vector */
vec
tor
to_cartesian
(
polarVector
v1
);
/* Convert a Polar vector to a Cartesian vector */
vec
2
add_vector
(
vec
2
v1
,
vec
2
v2
);
/* Add a vector to another and return the result as a vector */
vec
2
sub_vector
(
vec
2
v1
,
vec
2
v2
);
/* Substract a vector to another vector and return the result as a vector */
vec
2
mult_by_scalar
(
vec
2
v1
,
int
scalar
);
/* Multiply a vector by a scalar */
double
dot_multiply
(
vec
2
v1
,
vec
2
v2
);
/* Multiply two vectors with the dot product formula */
double
norm
(
vec
2
v1
);
/* Get the norm of a given vector */
polarVector
to_polar
(
vec
2
v1
);
/* Convert a Cartesian vector to a Polar vector */
vec
2
to_cartesian
(
polarVector
v1
);
/* Convert a Polar vector to a Cartesian vector */
/* For the moment, the cross product hasn't be inplemented because it only work with vectors on 3 dimentionals plan. */
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment