2008年10月24日金曜日

Java3Dを使おう(2)

Java3DでSunSPOTを3Dで作成しようと現在勉強中(ってほど時間かける余裕ないけど)

現在までの成果はこんな感じ..
日本語のまとまったドキュメントが欲しいです...なんか本でも買おうかな(笑)



ついでにソースリスト.


package java3dsuspot;

import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;
import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
import javax.swing.*;
import java.awt.*;

import javax.media.j3d.*;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.universe.*;
import javax.vecmath.*;

/**
* Java3DでSunSPOTを作成
* @author taka
*/
public class Spot3D extends JFrame{
private TransformGroup objTrans1;
private TransformGroup objTrans2;
private SimpleUniverse uni;
private Canvas3D canvas;
private RotationInterpolator rotatX;
private RotationInterpolator rotatY;
private RotationInterpolator rotatZ;
private Alpha alpha;
/**
* コンストラクタ
*/
public Spot3D(){
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
canvas = new Canvas3D(config);
getContentPane().add(canvas, BorderLayout.CENTER);

BranchGroup scene = createSceneGraph();
uni = new SimpleUniverse(canvas);
uni.getViewingPlatform().setNominalViewingTransform(); // 視点の設定
uni.addBranchGraph(scene);

setOrbitBehavior();

setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
/**
* 3Dオブジェクトの作成
*/
public BranchGroup createSceneGraph() {
// ObjectTreeの根の用意
BranchGroup objRoot = new BranchGroup();
// 座標軸を追加
Point3d[] vertex = new Point3d[6];

vertex[0] = new Point3d(-10.0, 0.0, 0.0); vertex[1] = new Point3d(10.0, 0.0, 0.0);
vertex[2] = new Point3d(0.0, -10.0, 0.0); vertex[3] = new Point3d(0.0, 10.0, 0.0);
vertex[4] = new Point3d(0.0, 0.0, -10.0); vertex[5] = new Point3d(0.0, 0, 10.0);

LineArray geometry = new LineArray(vertex.length,
GeometryArray.COORDINATES | GeometryArray.COLOR_3);
geometry.setCoordinates(0, vertex);

geometry.setColor(0, new Color3f(Color.red));
geometry.setColor(1, new Color3f(Color.red));
geometry.setColor(2, new Color3f(Color.cyan));
geometry.setColor(3, new Color3f(Color.cyan));
geometry.setColor(4, new Color3f(Color.blue));
geometry.setColor(5, new Color3f(Color.blue));

Shape3D shape = new Shape3D(geometry);
objRoot.addChild(shape);

// 光源を追加
objRoot.addChild(createLight1());
objRoot.addChild(createLight2());
// 新しい座標系
Transform3D t3d1 = new Transform3D();
Matrix4f mat = new Matrix4f(
1.0f,0.0f,0.0f,0.5f,
0.0f,1.0f,0.0f,0.0f,
0.0f,0.0f,1.0f,0.0f,
0.0f,0.0f,0.0f,1.0f
);
t3d1.set(mat);
Transform3D t3d2 = new Transform3D();
Matrix4f mat2 = new Matrix4f(
1.0f,0.0f,0.0f,0.0f,
0.0f,1.0f,0.0f,0.0f,
0.0f,0.0f,1.0f,0.0f,
0.0f,0.0f,0.0f,1.0f
);
t3d2.set(mat2);
// transグループの作成
objTrans1 = new TransformGroup(t3d1);// x方向+0.5
objTrans2 = new TransformGroup(t3d2);// 変更なし
// 許可
objTrans1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objTrans1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
/*
alpha = new Alpha(
1, Alpha.INCREASING_ENABLE, 0, 0, 3000, 400, 0, 3000, 400, 0);
Transform3D axis = new Transform3D();
axis.setTranslation(new Vector3f(0.0f, 0.0f, 0.0f));
//axis.rotX(Math.PI / 2);
RotationInterpolator rotat =
new RotationInterpolator(
alpha, objTrans2, axis,0.0f, (float) Math.PI / 2);
BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0);
rotat.setSchedulingBounds(bounds);
objTrans2.addChild(rotat);
*/
// key操作
KeyNavigatorBehavior knb = new KeyNavigatorBehavior(objTrans2);
BoundingSphere bounds = new BoundingSphere();
bounds.setRadius(100);
knb.setSchedulingBounds(bounds);
objTrans2.addChild(knb);

// objTreeに追加
objTrans2.addChild(objTrans1);
objRoot.addChild(objTrans2);

// Boxを作成
Appearance ap = new Appearance(); // 物体の質感
Material ma = new Material();
ma.setDiffuseColor(1.0f,1.0f,1.0f); // 色の設定(RGB)
ap.setMaterial(ma);// materialをappearanceにセット
Box box = new Box(0.5f,0.1f,0.3f,ap); // Boxの作成
Box box2 = new Box(0.08f,0.02f,0.3f,ap);


// 木に追加
objTrans2.addChild(box);
objTrans1.addChild(box2);
// コンパイル
objRoot.compile();

return objRoot;

}
/**
* 回転系
*/
public void rotation(){
// x軸
Transform3D axis = new Transform3D();
axis.setTranslation(new Vector3f(0.0f, 0.0f, 0.0f));
axis.rotZ(Math.PI / 2);
rotatX = new RotationInterpolator(
alpha, objTrans2, axis, 0.0f, (float) Math.PI * 2);
BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0);
rotatX.setSchedulingBounds(bounds);
objTrans2.addChild(rotatX);
}
/**
* 光源を作成
* @return
*/
private Light createLight1() {
// 平行光源
DirectionalLight light = new DirectionalLight(true,
new Color3f(1.0f, 1.0f, 1.0f), // 光源の色
new Vector3f(0.0f, 0.0f, -1.0f));// 光の向き
// AmbientLight light = new AmbientLight(true,new Color3f(1.0f,1.0f,1.0f));
light.setInfluencingBounds(new BoundingSphere(new Point3d(), 100.0));// 中心点,影響する範囲

return light;
}
private Light createLight2() {
AmbientLight light = new AmbientLight(true, new Color3f(1.0f, 1.0f, 1.0f));
light.setInfluencingBounds(new BoundingSphere(new Point3d(), 100.0));// 中心点,影響する範囲
return light;
}
/**
* マウス操作
*/
public void setOrbitBehavior(){
OrbitBehavior orbit = new OrbitBehavior(canvas,OrbitBehavior.REVERSE_ALL);
orbit.setSchedulingBounds(
new BoundingSphere(new Point3d(0,0,0),100)
);
uni.getViewingPlatform().setViewPlatformBehavior(orbit);
}

}

2008年10月21日火曜日

この前,学会で見たやつがついに
http://www.fcl.fujitsu.com/release/2008/20080924.html

2008年10月18日土曜日

ボゴソート

いよいよ情報処理技術者試験が明日に迫りました.
僕含め研究室の同期,先輩が必死に勉強していることと思います.

そんな中,面白いソートアルゴリズムを発見しました.

ボゴソート

これもソートアルゴリズムのひとつだ,と言い張る勇気が僕もほしいです...

2008年10月15日水曜日

Java3Dを使おう(1)

最近,誰も記事を書かないので頑張って更新.
今日から少しJava3Dを使うことにしました.何に使うかはおいおい....

そろそろ"Operaerの憂鬱"とか連載記事が始まるのではないかとちょっと期待してます(笑)

Java3DはJava向けの3D描画API拡張です.何故かJ2SDKとかには入ってません.どーいうことかって言うと別途自分でインストールしてねってことです.

現在はjava.net内のprojectページが配布サイトです.
この辺から自分に合ったのを落としてきて適当にインストールします.

続く(?)

2008年10月12日日曜日

SunSPOT SDK ver4.0

久々の更新.

SunSPOT SDK ver4.0がいつの間にか正式にリリースされていました.
この辺参照.

以下,公式のBBSからの引用です.

Version 4.0 of the Sun SPOT SDK (the Blue release) is now available for downloading via SPOT Manager. To install it go to http://www.sunspotworld.com/SPOTManager/ and click on the SPOT icon. In the SPOT Manager open the Preferences tab and select "General Release Updates". Then go to the SDK tab and choose "v4.0 Blue (Latest Update)".

Major changes include:

* Upgrading a SPOT to Blue will delete any user set properties because the section of SPOT flash memory used to store properties has changed.
* Can now deploy multiple application suites to be loaded & run on a SPOT
* The SPOTWorld application is now called Solarium
* Demos are now included as part of the SDK: /Demos
* Three new demos have been added: DatabaseDemo, HTTPDemo and SendDataDemo
* New SPOT-Utilities directory containing utility applications to calibrate the accelerometer, sniff packets and test the eDemo sensors
* New persistent system properties to specify default radio channel, pan id and transmit power.
* Several new ant commands
* SPOT host applications should no longer use the SPOT class; use RadioFactory instead.
* Bugs in the eDemo board UART code have been fixed and the UART API changed.
* The RoutingManager has been fixed so that its normal operation no longer prevents the SPOT from going into deep sleep.
* Many SPOT library jar files were renamed or moved
* Almost 200 bugs fixed

Please see the Release Notes for more details.

When reporting problems or bugs please be sure to specify which version of the SDK you are using.