Skip to content

Commit c228d72

Browse files
committed
Simplify unbend( and bend() usage
1 parent 9e4526b commit c228d72

19 files changed

+188
-209
lines changed

Diff for: NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export(Rota_Inverse_YZ)
99
export(Rota_YZ)
1010
export(XYZ_Vers_Agl)
1111
export(bend)
12+
export(distance_weight_sine)
1213
export(plot_bending)
1314
export(plot_bending_3d)
1415
export(read_mat)

Diff for: R/XYZ_Vers_Agl.R

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
#'
1414
#' @examples
1515
#' filepath = system.file("extdata/6_EW01.22_17_kanan.txt", package = "biomech")
16-
#' df = read_mat(filepath)
17-
#' df = unbend(2000,400,df)
16+
#' df = unbend(read_mat(filepath))
1817
#' XYZ_Vers_Agl(df$x,df$y,df$z)
1918
#'
2019
XYZ_Vers_Agl = function(vecX, vecY, vecZ){

Diff for: R/application_distance.R

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#' Weight distance of application
2+
#'
3+
#' Computes the distance of application of the right/left weights using a sine
4+
#' evolution along the beam.
5+
#'
6+
#' @param x X position of the segments
7+
#' @param dF Amplitude (m)
8+
#'
9+
#' @return The distance at which the left/right weights are applied from the beam axis. Both
10+
#' sides are considered equal.
11+
#' @export
12+
#'
13+
#' @examples
14+
#' distance_weight_sine(c(1,2,3))
15+
distance_weight_sine = function(x,dF = 0.1){
16+
sin(x / x[length(x)] * pi) * dF
17+
}

Diff for: R/bend.R

+39-18
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@
33
#' Compute the deformation by applying both bending and torsion
44
#'
55
#' @param data Point data.frame (see details).
6+
#' @param elastic_modulus Elasticity modulus (bending, MPa)
7+
#' @param shear_modulus shear modulus (torsion, MPa)
68
#' @param step Length of the segments that discretize the object (m).
79
#' @param points Number of points used in the grid discretizing the section.
810
#' @param iterations Number of iterations to compute the torsion and bending
911
#' @param verbose Boolean. Return information during procress?
1012
#'
1113
#' @details The data argument is a [data.frame()] with each row being a point and each column being:
12-
#' - x: x coordinate
13-
#' - y: y coordinate
14-
#' - z: z coordinate
15-
#' - type: section type. 1: triangle (bottom-oriented); 2 : rectangle; 3 : triangle (top-oriented);
14+
#' - x: x coordinate of the segment
15+
#' - y: y coordinate of the segment
16+
#' - z: z coordinate of the segment
17+
#' - type: section type of the segment. 1: triangle (bottom-oriented); 2 : rectangle; 3 : triangle (top-oriented);
1618
#' 4 : ellipsis; 5 : circle.
17-
#' - width (m): section width
18-
#' - height (m): section height
19-
#' - mass (kg): mass of the section at the point
20-
#' - mass_right (kg): mass carried by the object, on the right side
21-
#' - mass_left (kg): mass carried by the object, on the left side
22-
#' - elastic_modulus (MPa): elasticity modulus (bending)
23-
#' - shear_modulus (MPa): shear modulus (torsion)
24-
#' - inclination (degree): insertion angle of the first section (only first value is used)
25-
#' - distance_application (m): application distances for the left and right weights
19+
#' - width (m): segment section width
20+
#' - height (m): segment section height
21+
#' - mass (kg): mass of the segment
22+
#' - mass_right (kg): mass carried by the segment, on the right side
23+
#' - mass_left (kg): mass carried by the segment, on the left side
24+
#' - torsion (degree): initial torsion angle of the segment
25+
#' - distance_application (m): application distances for the left and right weights (see e.g. [distance_weight_sine()])
2626
#'
2727
#' @return A [data.frame()]:
2828
#' - x (m): X coordinate
@@ -37,12 +37,17 @@
3737
#'
3838
#' @examples
3939
#' filepath = system.file("extdata/6_EW01.22_17_kanan.txt", package = "biomech")
40+
#' df = read_mat(filepath)
4041
#' # Un-bending the field measurements:
41-
#' df = unbend(2000,400, read_mat(filepath))
42+
#' df = unbend(df)
43+
#'
44+
#' # Adding the distance of application of the left and right weight:
45+
#' df$distance_application = distance_weight_sine(df$x)
4246
#'
4347
#' # (Re-)computing the deformation:
44-
#' df_bend = bend(df, step = 0.02, points = 100, iterations = 15, verbose = TRUE)
45-
bend = function(data, step = 0.02, points = 100,
48+
#' df_bend = bend(df, elastic_modulus = 2000, shear_modulus = 400, step = 0.02, points = 100,
49+
#' iterations = 15, verbose = TRUE)
50+
bend = function(data,elastic_modulus,shear_modulus, step = 0.02, points = 100,
4651
iterations = 15, verbose = TRUE){
4752

4853
vecRotFlex = matrix(0, ncol = 1, nrow = 3)
@@ -91,10 +96,26 @@ bend = function(data, step = 0.02, points = 100,
9196
step = distTotale / (Nlin - 1)
9297
vecDist = (0 : (Nlin - 1)) * step
9398

94-
vMOE = data$elastic_modulus
99+
if(length(elastic_modulus) != nrow(data)){
100+
if(length(elastic_modulus) == 1){
101+
elastic_modulus = rep(elastic_modulus, nrow(data))
102+
}else{
103+
stop("elastic_modulus argument should be of length 1 or equal to `nrow(data)`")
104+
}
105+
}
106+
107+
if(length(shear_modulus) != nrow(data)){
108+
if(length(shear_modulus) == 1){
109+
shear_modulus = rep(shear_modulus, nrow(data))
110+
}else{
111+
stop("shear_modulus argument should be of length 1 or equal to `nrow(data)`")
112+
}
113+
}
114+
115+
vMOE = elastic_modulus
95116
vecMOE = stats::approx(c(0,distLineique), c(vMOE[1], vMOE), vecDist, method = 'linear')$y
96117

97-
vG = data$shear_modulus
118+
vG = shear_modulus
98119
vecG = stats::approx(c(0,distLineique), c(vG[1], vG), vecDist, method = 'linear')$y
99120

100121
vAngle_Tor = data$torsion * pi / 180 # (radian)

Diff for: R/plot_bent.R

+14-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,20 @@
1010
#' @importFrom rlang .data
1111
#'
1212
#' @examples
13-
#' filepath = system.file("extdata/6_EW01.22_17_kanan.txt", package = "biomech")
13+
#' file_path = system.file("extdata/6_EW01.22_17_kanan.txt", package = "biomech")
14+
#' field_data = read_mat(file_path)
1415
#' # Un-bending the field measurements:
15-
#' df = unbend(2000,400, read_mat(filepath))
16-
#' df_bend = bend(df, step = 0.02, points = 100, iterations = 15, verbose = TRUE)
17-
#' plot_bending(df_bend,df)
16+
#' df_unbent = unbend(field_data)
17+
#'
18+
#' # Adding the distance of application of the left and right weight:
19+
#' df_unbent$distance_application = distance_weight_sine(df_unbent$x)
20+
#'
21+
#' # (Re-)computing the deformation:
22+
#' df_bent = bend(df_unbent, elastic_modulus = 2000, shear_modulus = 400, step = 0.02,
23+
#' points = 100, iterations = 15, verbose = TRUE)
24+
#'
25+
#' # And finally, plotting:
26+
#' plot_bending(df_bent,df_unbent)
1827
plot_bending = function(bent, unbent = NULL, ...){
1928
plot_range = range(bent$x,bent$y,bent$z,unbent$x,unbent$y,unbent$z)
2029
plot_range_max = max(plot_range)
@@ -55,7 +64,7 @@ plot_bending_3d = function(bent, unbent = NULL, ...){
5564
{
5665
if(!is.null(unbent)){
5766
plotly::add_trace(p = ., data = unbent, x = ~x, y = ~y, z = ~z,
58-
name = "Initial state")
67+
name = "Un-bent")
5968
}else{
6069
.
6170
}

Diff for: R/unbend.R

+20-84
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,60 @@
11
#' Unbend
22
#'
3-
#' Takes measurements from field that are already bent (torsion + bending) and transform them
4-
#' to a straight line (un-bending).
5-
#' Mainly used to compute the matrix for input to [bend()] from experimental points.
3+
#' Remove torsion and bending of a bent beam, i.e. transform it
4+
#' into a straight line, while keeping its insertion angle (inclination angle of the first segment).
5+
#'
6+
#' @note Mainly used to compute the matrix for input to [bend()] from experimental points.
67
#'
7-
#' @param MOE Elastic modulus
8-
#' @param CIS Shear modulus
98
#' @param df Experimental data (usually read using [read_mat()], see details)
109
#'
1110
#' @details df should be a formatted [data.frame()], with each row being a point and each columns being:
1211
#' - distance (m): distance between the previous point and this point (first value should be positive)
13-
#' - type: section type. 1: triangle (bottom-oriented); 2 : rectangle; 3 : triangle (top-oriented);
14-
#' 4 : ellipsis; 5 : circle.
15-
#' - width (m): section width
16-
#' - height (m): section height
1712
#' - inclination (degree): insertion angle of the first point (only first value is used)
18-
#' - torsion (degree): torsion angle
19-
#' - x: x coordinate (optional, only for plotting)
20-
#' - y: y coordinate (optional, only for plotting)
21-
#' - z: z coordinate (optional, only for plotting)
22-
#' - mass (kg): mass of the section at the point
23-
#' - mass_right (kg): mass carried by the object, on the right side
24-
#' - mass_left (kg): mass carried by the object, on the left side
2513
#'
26-
#' @return A [data.frame()]:
14+
#' @return The input [data.frame()] with modified (or enriched with):
2715
#' - x: x coordinate
2816
#' - y: y coordinate
2917
#' - z: z coordinate
30-
#' - type: section type. 1: triangle (bottom-oriented); 2 : rectangle; 3 : triangle (top-oriented);
31-
#' 4 : ellipsis; 5 : circle.
32-
#' - width (m): section width
33-
#' - height (m): section height
34-
#' - mass (kg): mass of the section at the point
35-
#' - mass_right (kg): mass carried by the object, on the right side
36-
#' - mass_left (kg): mass carried by the object, on the left side
37-
#' - elastic_modulus (MPa): elasticity modulus (bending)
38-
#' - shear_modulus (MPa): shear modulus (torsion)
39-
#' - inclination (degree): insertion angle of the first section (only first value is used)
40-
#' - distance_application (m): application distances for the left and right weights
4118
#'
4219
#' @export
4320
#'
4421
#' @examples
4522
#' \dontrun{
4623
#' filepath = system.file("extdata/6_EW01.22_17_kanan.txt", package = "biomech")
4724
#' df = read_mat(filepath)
48-
#' unbend(MOE = 2000, CIS = 400, df = df)
25+
#' unbend(df = df)
4926
#' }
50-
unbend = function(MOE, CIS, df){
27+
unbend = function(df){
5128

52-
# La distance entre les points ne peut pas etre nulle
29+
# The distance between points can't be 0
5330
iD0 = which(df$distance == 0)
5431
if(length(iD0)>0){
5532
df$distance[iD0] = 1e-3 # (m)
5633
}
57-
# Coordonnees des points (etat non deforme)
34+
5835
Distance = df$distance
36+
37+
# cumulated distance of each segment:
5938
XDistance = cumsum(unlist(Distance))
6039

40+
# keeping the inclination of the first segment (i.e. insertion angle):
6141
Agl_Y = df$inclination[1] * pi / 180
6242
Agl_Z = 0
6343

64-
NpointsExp = length(Distance)
44+
# Initializing in case they are missing:
45+
df$x = df$y = df$z = 0
6546

66-
X = Y = Z = rep(0, NpointsExp)
67-
68-
# Segment de droite oriente dans un espace 3D
69-
for(iter in 1 : NpointsExp){
47+
# Coordinates of the points (un-bent state):
48+
for(iter in 1 : nrow(df)){
7049
OP = c(XDistance[[iter]], 0, 0)
7150
vecRot = Rota_YZ(OP, Agl_Y, Agl_Z)
7251

73-
X[iter] = vecRot[1]
74-
Y[iter] = vecRot[2]
75-
Z[iter] = vecRot[3]
52+
df$x[iter] = vecRot[1]
53+
df$y[iter] = vecRot[2]
54+
df$z[iter] = vecRot[3]
7655
}
7756

78-
# Affichage graphique
79-
# if (gph == 1)
80-
# figure, hold on
81-
# plot3(df(iX,:), df(iY,:), df(iZ,:), 'bo--')
82-
# plot3(X, Y, Z, 'ko--')
83-
# xlabel('X (m)'), ylabel('Y (m)'), zlabel('Z (m)')
84-
# title('Etat deforme (mesures experimentales) et non deforme')
85-
# axis('equal'), grid on, view(45, 25)
86-
# end
87-
88-
# Section
89-
TypeSection = df$type
90-
Base = df$width
91-
Hauteur = df$height
92-
93-
# Poids
94-
PoidsTige = df$mass
95-
PoidsFeuillesDroite = df$mass_right
96-
PoidsFeuillesGauche = df$mass_left
97-
98-
# Elasticite
99-
# Valeur constante
100-
ModuleElasticite = rep(MOE, NpointsExp)
101-
ModuleCisaillement = rep(CIS, NpointsExp)
102-
103-
# Orientations initales des sections
104-
AngleSection = df$torsion
105-
106-
# Distance d'application du poids des feuilles
107-
# Evolution sinus, valeur arbitraire de dF
108-
# Le ratio dF/CIS est optimise
109-
dF = 0.1
110-
DAppliPoidsFeuil = sin(XDistance / utils::tail(XDistance,1) * pi) * dF
111-
112-
#===============================================================================
113-
# Resultats
114-
df = data.frame(x= X, y = Y, z = Z, type = TypeSection, width = Base,
115-
height = Hauteur, mass = PoidsTige, mass_right = PoidsFeuillesDroite,
116-
mass_left = PoidsFeuillesGauche,
117-
elastic_modulus = ModuleElasticite,
118-
shear_modulus = ModuleCisaillement,
119-
torsion = AngleSection,
120-
distance_application = DAppliPoidsFeuil)
121-
class(df) = append("unbent",class(df))
12257
df
12358
}
12459

60+

Diff for: README.Rmd

+11-6
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,32 @@ Here is an example usage:
3737

3838
```{r example}
3939
library(biomech)
40-
filepath = system.file("extdata/6_EW01.22_17_kanan.txt", package = "biomech")
40+
file_path = system.file("extdata/6_EW01.22_17_kanan.txt", package = "biomech")
41+
field_data = read_mat(file_path)
4142
# Un-bending the field measurements:
42-
df = unbend(2000,400, read_mat(filepath))
43+
df_unbent = unbend(field_data)
44+
45+
# Adding the distance of application of the left and right weight:
46+
df_unbent$distance_application = distance_weight_sine(df_unbent$x)
47+
4348
# (Re-)computing the deformation:
44-
df_bend = bend(df, step = 0.02, points = 100, iterations = 15, verbose = TRUE)
49+
df_bent = bend(df_unbent, elastic_modulus = 2000, shear_modulus = 400, step = 0.02, points = 100, iterations = 15, verbose = TRUE)
4550
46-
df_bend
51+
df_bent
4752
```
4853

4954
You can plot the results using:
5055

5156
```{r}
52-
plot_bending(bent = df_bend, unbent = df)
57+
plot_bending(bent = df_bent, unbent = df_unbent)
5358
```
5459

5560
Note that the `unbent` argument is optional but can be very usefull to compare with the initial conditions.
5661

5762
You can even make 3d plots using `plot_bent_3d()`:
5863

5964
```{r eval=FALSE}
60-
plot_bending_3d(df_bend,df)
65+
plot_bending_3d(df_bent,df_unbent)
6166
```
6267

6368
```{r echo=FALSE}

Diff for: man/XYZ_Vers_Agl.Rd

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)