Converting OBJ data to CSS3D - html

I found a ton of formulae and what not, but 3D isn't my forte so I'm at a loss of what specifically to use. My goal is to convert the data in an 3D .obj file (vertices, normals, faces) to CSS3D (width, height, rotateX,Y,Z and/or similar transforms).
For example 2 simple planes
g plane1
# simple along along Z axis
v 0.0 0.0 0.0
v 0.0 0.0 1.0
v 0.0 1.0 1.0
v 0.0 1.0 0.0
g plane2
# plane rotated 90 degrees along Y-axis
v 0.0 0.0 0.0
v 0.0 1.0 0.0
v 1.0 1.0 0.0
v 1.0 0.0 0.0
f 1 2 3 4
f 5 6 7 8
Could this data be converted to:
#plane1 {
width: X;
height: Y;
transform: rotateX(Xdeg) rotateY(Ydeg) rotateZ(Zdeg) translateZ(Zpx)
}
#plane2 {
width: X;
height: Y;
transform: rotateX(Xdeg) rotateY(Ydeg) rotateZ(Zdeg) translateZ(Zpx)
}
/* Or something equivalent such as transform: matrix3d() */
The core question is how to get the X/Y/Z-rotation of a 4 point plane from it's matrix of x,y,z coordinates?
UPDATE #1 - 11/12/12
So based on the answers provided, I've come across the unoptimized function from http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToEuler/index.htm below:
/*
-v 0.940148 -0.847439 -1.052535
-v 0.940148 -0.847439 0.947465
-v -1.059852 -0.847439 0.947465
-v -1.059852 -0.847439 -1.052535
-v 0.940148 1.152561 -1.052534
-v 0.940147 1.152561 0.947466
-v -1.059852 1.152561 0.947465
v -1.059852 1.152561 -1.052535
f 1 2 3 4
f 5 8 7 6
f 1 5 6 2
f 2 6 7 3
f 3 7 8 4
f 5 1 4 8
*/
var f = {
'm00' : 0.940148,
'm01' : -0.847439,
'm02' : -1.052535,
'm10' : 0.940148,
'm11' : -0.847439,
'm12' : 0.947465,
'm20' : -1.059852,
'm21' : -0.847439,
'm22' : 0.947465
}
// Assuming the angles are in radians.
if (f.m10 > 0.998) { // singularity at north pole
heading = Math.atan2(f.m02, f.m22);
attitude = Math.PI/2;
bank = 0;
} else if (f.m10 < -0.998) { // singularity at south pole
heading = Math.atan2(f.m02,f.m22);
attitude = -Math.PI/2;
bank = 0;
} else {
heading = Math.atan2(-f.m20, f.m00);
bank = Math.atan2(-f.m12, f.m11);
attitude = Math.asin(f.m10);
}
I'm getting results, but I'm not sure if my calculations are correct and I'm also getting mixed responses on what corresponds to which axis. Is it heading = y, bank = x, and attitude = z? I'm also converting each to degrees if that matters.

Read this http://www.songho.ca/opengl/gl_matrix.html It explains pretty much everything and there is implementation.
Beside that the CSS 3D solution will have lower performance(order of magnitude) mainly because each pice of represented surface is DOM element, it's also highly limited - you can find numerous materials about this issue(Google IO records for example)
If you need declarative 3D framework you might want to look at x3dom
To draw a 3D box you just need to include x3dom js script and embed this declaration in your page:
<body>
<h1>Hello X3DOM World</h1>
<x3d width="400" height="300">
<scene>
<shape>
<box></box>
</shape>
</scene>
</x3d>
</body>
It will parse <x3d> tags on your page and generate proper WebGL or Flash implementation with the good performance.
x3d has way to import assets from Blender, Maya and 3ds Max.
Here is some good reading: x3domIntroTutorial.pdf
IE 11 will support WebGL and IE10 will autoupdate to IE 11 so only non-supporting desktop browser(disabled by default) will be Safari. Apple will be forced to enable it by default. With full desktop support it won't take too long to get full mobile because it's highly competitive market. And we have highly accessible WebGL framework like three.js. So there is no sense in doing it with CSS 3D
UPDATE: iOS 8 Safari will enable WebGL support by default: http://caniuse.com/webgl

Related

Contrast emmeans: post-hoc t-test as the average differences of the differences between baseline and treatment periods

I am using the lme4 package in R to undertake linear mixed effect models (LMM). Essentially all participants received two interventions (an intervention treatment and a placebo (control)) and were separated by a washout period. However, the order or sequence they received the interventions differed.
An interaction term of intervention and visit was included in the LMM with eight levels including all combinations of intervention (2 levels: control and intervention) and visit (4 levels: visit 1=baseline 1, visit 2, visit 3=post-randomization baseline 2, visit 4).
My question is how do I determine the intervention effect by a post-hoc t-test as the average differences of the differences between interventions, hence between visits 1 and 2 and between visits 3 and 4. I also want to determine the effects of the intervention and control compared to baseline.
Please see code below:
model1<- lmer(X ~ treatment_type:visit_code + (1|SID) + (1|SID:period), na.action= na.omit, data = data.x)
emm <- emmeans(model1 , ~treatment_type:visit_code)
My results of model 1 is:
emm
treatment_type visit_code emmean SE df lower.CL upper.CL
Control T0 -0.2915 0.167 26.0 -0.635 0.0520
Intervention T0 -0.1424 0.167 26.0 -0.486 0.2011
Control T1 -0.2335 0.167 26.0 -0.577 0.1100
Intervention T1 0.0884 0.167 26.0 -0.255 0.4319
Control T2 0.0441 0.167 26.0 -0.299 0.3876
Intervention T2 -0.2708 0.168 26.8 -0.616 0.0748
Control T3 0.1272 0.167 26.0 -0.216 0.4708
Intervention T3 0.0530 0.168 26.8 -0.293 0.3987
Degrees-of-freedom method: kenward-roger
Confidence level used: 0.95
I first created a matrix/ vectors:
#name vectors
Control.B1<- c(1,0,0,0,0,0,0,0) #control baseline 1 (visit 1)
Intervention.B1<- c(0,1,0,0,0,0,0,0) #intervention baseline 1 (visit 1)
Control.A2<- c(0,0,1,0,0,0,0,0) #post control 1 (visit 2)
Intervention.A2<- c(0,0,0,1,0,0,0,0) #post intervention 1 (visit 2)
ControlB3<- c(0,0,0,0,1,0,0,0) #control baseline 2 (visit 3)
Intervention.B3<- c(0,0,0,0,0,1,0,0) #intervention baseline 2 (visit 3)
Control.A4<- c(0,0,0,0,0,0,1,0) #post control 2 (visit 4)
Intervention.A4<- c(0,0,0,0,0,0,0,1) #post intervention 2 (visit 4)
Contbaseline = (Control.B1 + Control.B3)/2 # average of control baseline visits
Intbaseline = (Intervention. B1 + Intervention.B3)/2 # average of intervention baseline visits
ControlAfter= (Control.A2 + Control.A4)/2 # average of after control visits
IntervAfter= (Intervention.A2 + Intervention.A4)/2 # average of after intervention visits
Control.vs.Baseline = (ControlAfter-Contbaseline)
Intervention.vs.Baseline = (IntervAfter-Intbaseline)
Control.vs.Intervention = ((Control.vs.Baseline)-(Intervention.vs.Baseline))
the output of these are as follows:
> Control.vs.Baseline
[1] -0.5 0.0 0.5 0.0 -0.5 0.0 0.5 0.0
> Intervention.vs.Baseline
[1] 0.0 -0.5 0.0 0.5 0.0 -0.5 0.0 0.5
> Control.vs.Intervention
[1] -0.5 0.5 0.5 -0.5 -0.5 0.5 0.5 -0.5
Is this correct to the average differences of the differences between baseline and treatment periods?
Many thanks in advance!
A two-period crossover is the same as a repeated 2x2 Latin square. My suggestion for future such experiments is to structure the data accordingly, using variables for sequence (rows), period (columns), and treatment (assigned in the pattern (A,B) first sequence and (B,A) second sequence. The subjects are randomized to which sequence they are in.
So with your data, you would need to add a variable sequence that has the level AB for those subjects who receive the treatment sequence A, A, B, B, and level BA for those who receive B, B, A, A (though I guess the 1st and 3rd are really baseline for everybody).
Since there are 4 visits, it helps keep things sorted if you recode that as two factors trial and period, as follows:
visit trial period
1 base 1
2 test 1
3 base 2
4 test 2
Then fit the model with formula
model2 <- lmer(X ~ (sequence + period + treatment_type) * trial +
(1|SID:sequence), ...etc...)
The parenthesized part is the standard model for a Latin square. Then the analysis can be done without custom contrasts as follows:
RG <- ref_grid(model2) # same really as emmeans() for all 4 factors
CHG <- contrast(RG, "consec", simple = "trial")
CHG <- update(CHG, by = NULL, infer = c(TRUE, FALSE))
CHG contains the differences from baseline (trial differences for each combination of the other three factors. The update() step removes the by variables saved from contrast(). Now, we can get the marginal means and comparisons for each factor:
emmeans(CHG, consec ~ treatment_type)
emmeans(CHG, consec ~ period)
emmeans(CHG, consec ~ sequence)
These will be the same results you got the other way via custom contrasts. The one that was a difference of differences before is now handled by sequence. This works because in a 2x2 Latin square, the main effect of each factor is confounded with the two-way interaction of the other two factors.

How to plot the graph using MATLAB (or not matlab))?

I've got the function fi(ϕ)=γi+sin(2⋅sinϕ) for i=1,2 where γ1=0.01 and γ2=0.02
ϕ1(0)=0.1 and ϕ2(0)=0.2
ϕ1/dt=f1(ϕ)+d⋅sin(ϕ2−ϕ1)
ϕ2/dt=f2(ϕ)+d⋅sin(ϕ1−ϕ2)
where d=0.1
So there should be something like for example this table:
t | ϕ1 | ϕ2
0.00 | 0.1 |0.2
0.01 | ... |...
0.02 | ... |...
...
100.00| ... | ...
And so using the received values it's needed to plot a graph by the coordinates
So the question is how to plot the function ϕ2(ϕ1) on the the following graph using MATLAB?
So the story of the system might be that you start with two uncoupled and slightly different equations
ϕ1/dt=f1(ϕ1)
ϕ2/dt=f2(ϕ2)
and connect them with a coupling or exchange term sin(ϕ2-ϕ1),
ϕ1/dt=f1(ϕ1)+d⋅sin(ϕ2−ϕ1)
ϕ2/dt=f2(ϕ2)+d⋅sin(ϕ1−ϕ2)
In a matlab script you would implement this as
y0 = [ 0.1; 0.2 ];
[T,Y] = ode45(eqn,[0, 100], y0);
plot(Y(:,1),Y(:,2));
function dy_dt = eqn(t,y)
d = 0.1;
g = [ 0.01; 0.02 ];
f = g+sin(2*sin(y));
exch = d*sin(y(2)-y(1));
dy_dt = f+[d;-d];
end%function
which gives almost a diagonal line ending at [pi; pi]. With a stronger coupling constant d this becomes slightly more interesting.
You can give the parameters as parameter arguments, then you have to declare them via odeset in an options object, or use anonymous functions to bind the parameters in the solver call.

Invalid subscript "list" error when converting from JSON to Dataframe using R

I was following the instructions mentioned in the following question to convert JSON data to a dataframe using RJSONIO package. Link below:
How to convert JSON to Dataframe
Below is the JSON summary of my data, each field contains equal number of values, somewhere around 50,000. The value in color field is of type list, my guess is that is what is causing the problem.
json
title: chr
remaining: chr
color: list()
brand: chr
modelnum: chr
size: chr
I am attaching a sample set of JSON values, if anyone on the community can shed some light on how to model this into a dataframe, it'll be great!
Sample JSON data:
{"title":"oneplus 3","remaining":"","color":[],"brand":"OnePlus","modelnum":"OnePlus 3","size":""}
{"title":"oneplus 3 (soft gold, 64 gb)","remaining":"(soft )","color":["gold"],"brand":"OnePlus","modelnum":"OnePlus 3","size":"64 gb"}
{"title":"deal 1:oneplus 3 (graphite, 64gb) 6gb ram 4g lte - 1 year manufacture warranty","remaining":"deal 1: 6gb ram 4g lte - 1 year manufacture warranty","color":["graphite"],"brand":"OnePlus","modelnum":"OnePlus 3","size":"64gb"}
{"title":"oneplus 3 (graphite, 64 gb)","remaining":"","color":["graphite"],"brand":"OnePlus","modelnum":"OnePlus 3","size":"64 gb"}
{"title":"xiaomi redmi note 3 32gb","remaining":"","color":[],"brand":"Xiaomi","modelnum":"Redmi Note 3","size":"32gb"}
{"title":"xiaomi redmi note 3 (grey 32 gb) mobile phone","remaining":"mobile phone","color":["grey"],"brand":"Xiaomi","modelnum":"Redmi Note 3","size":"32 gb"}
{"title":"xiaomi redmi note 3 new (6 month brand warranty)","remaining":"new (6 month brand warranty)","color":[],"brand":"Xiaomi","modelnum":"Redmi Note 3","size":""}
{"title":"xiaomi redmi note 3 (gold 32gb) mobile phone","remaining":"mobile phone","color":["gold"],"brand":"Xiaomi","modelnum":"Redmi Note 3","size":"32gb"}
{"title":"xiaomi redmi note 3 (dark grey) (32gb)","remaining":"","color":["dark grey"],"brand":"Xiaomi","modelnum":"Redmi Note 3","size":"32gb"}
{"title":"mi redmi note 3 32gb dark grey","remaining":"mi","color":["dark grey"],"brand":"Xiaomi","modelnum":"Redmi Note 3","size":"32gb"}
{"title":"xiaomi redmi note 3 (gold, 32gb)","remaining":"","color":["gold"],"brand":"Xiaomi","modelnum":"Redmi Note 3","size":"32gb"}
R-code:
library(RJSONIO)
json <- fromJSON(file_path_for_the_above_data, nullValue = NA)
dat <- lapply(json, function(j) {
as.data.frame(replace(j, sapply(j, is.list), NA))
})
This is where the error occurs.
Error in replace(j, sapply(j, is.list), NA) :
invalid subscript type 'list'
Thank you.
The issue is with the wrong format of JSON, fixing the JSON array basically did the trick.

Geostationary orbit with pyephem?

I am trying to create a geostationary orbit in pyephem (_n = 1.0 revolutions per day). I would like to verify that it is geostationary by placing an observer directly below the satellite and verifying that alt='90.0' and az=0. For my test I am placing the observer on the equator at 100 deg W longitude. Here is my code:
import ephem
sat = ephem.EarthSatellite()
sat._n = 1.0
sat._e = 0.0
sat._inc = 0.0
sat._raan = '-100.0'
sat._ap = 0.0
sat._M = 0.0
obs = ephem.Observer()
obs.lat = 0.0
obs.lon = '-100.0'
obs.date = '2014/10/16 00:00:00'
sat.compute(obs)
print "obs position: lat=%s lon=%s date=%s" % \
(obs.lat, obs.lon, obs.date)
print "sat orbit: n=%s e=%s inc=%s raan=%s ap=%s M=%s" % \
(sat._n, sat._e, sat._inc, sat._raan, sat._ap, sat._M)
print "sat position: alt=%s az=%s ra=%s dec=%s sublat=%s sublong=%s" % \
(sat.alt, sat.az, sat.ra, sat.dec, sat.sublat.norm, sat.sublong.norm)
and the output:
> obs position: lat=0:00:00.0 lon=-100:00:00.0 date=2014/10/16 00:00:00
> sat orbit: n=1.0 e=0.0 inc=0:00:00.0 raan=-1:44:43.2 ap=0:00:00.0 M=0:00:00.0
> sat position: alt=-90:00:00.0 az=0:00:00.0 ra=6:57:44.67 dec=0:00:00.0 sublat=1389660529:33:00.8 sublong=335:33:55.8
I find that changing the observer longitude does not change the output. I expect that sat._raan sets the overhead position (sat.sublong) of the satellite, but this also has no affect on the output. I consistently get alt=-90:00:00.0 az=0:00:00.0. (Towards center of Earth) and sublat, sublong don't make any sense.
Update
The reason for the strange, unchanging output of sublat=1389660529:33:00.8 is due to the sat._epoch being '1899/12/31 12:00:00' by default, along with this issue. Setting sat._epoch = obs.date works around this, but I am still not sure how to achieve the goal of defining a geostationary orbit whose sky position is fixed above a chosen Earth coordinate.

Fill a region on a graph with no outline?

I'd like to fill a region on a graph plotted with octave, without any outline:
The fill command accepts a color argument that it respects for the filled area, but it doesn't seem to accept the 'LineColor' property to change the color of the line it draws around the filled area...
e.g.
fill([1 2 3 3 2 1], [1 0.5 1 -1 -1 -1], [0.9,0.9,0.9]); # line is black
fill([1 2 3 3 2 1], [1 0.5 1 -1 -1 -1], [0.9,0.9,0.9], 'LineColor', 'r') # hangs
I'm using octave-3.4.0 on OS X.
The patch command should do the job
verts = [0.2 0.4; ...
0.2 0.8; ...
0.8 0.8; ...
0.8 0.4];
faces = [1 2 3 4];
p = patch('Faces',faces,'Vertices',verts,'FaceColor','b','EdgeColor','none');
Of course you could also place it in one line ... ;-)