00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005 using System.IO;
00006 using Microsoft.Xna.Framework;
00007 using Microsoft.Xna.Framework.Audio;
00008 using Microsoft.Xna.Framework.Content;
00009 using Microsoft.Xna.Framework.GamerServices;
00010 using Microsoft.Xna.Framework.Graphics;
00011 using Microsoft.Xna.Framework.Input;
00012 using Microsoft.Xna.Framework.Media;
00013 using Microsoft.Xna.Framework.Net;
00014 using Microsoft.Xna.Framework.Storage;
00015 using visLU.Effects;
00016 using Microsoft.Xna.Framework.Graphics.PackedVector;
00017
00018 namespace visLU
00019 {
00023 class Data : DrawableGameComponent
00024 {
00025
00026 private RenderTarget2D frontTarget;
00027 private RenderTarget2D backTarget;
00028 private RenderTarget2D screenShotTarget;
00029 private ResolveTexture2D backBuffer_resolved;
00030 private Vector3 stepSize;
00031 private Vector4 scaleFactor;
00032 private int iterations;
00033 private float scaleSteps = 1.0f;
00034
00035 public float slice = 0.5f;
00036
00037
00038 private List<TransferControlPoint> colorControlPoints;
00039 private List<TransferControlPoint> alphaControlPoints;
00040 private Texture2D transferTexture;
00041 private Texture2D transferTextureAlpha;
00042
00043 private List<float> densityValues;
00044 private float[] densityArray;
00045 private float[] distinctDensityValuesArray;
00046 private int[] countDensityValuesArray;
00047
00048 private Vector3[] gradients;
00049
00050 private Matrix worldMatrix;
00051
00052
00053
00054 ushort sizeX, sizeY, sizeZ;
00055
00056 private UInt16[][][] data;
00057
00058 public VolumeShader shader;
00059
00060 private String fileName;
00061
00062 private Texture3D volumeData;
00063 private Texture2D frontFaces;
00064
00065 private Model cube;
00066
00067
00068 int texturesSaved = 0;
00069 GraphicsDevice device;
00070
00071
00072
00073 private Camera camera;
00074 private int currentView = 0;
00075
00076 #region Properties
00077 public Camera Camera
00078 {
00079 set { camera = value; }
00080 }
00081 public int CurrentCamera
00082 {
00083 set { currentView = value; }
00084 }
00085 #endregion
00086 #region Constructor
00087
00088
00089
00090
00091
00092 public Data(string _fileName, Game game) : base(game)
00093 {
00094 worldMatrix = Matrix.Identity;
00095
00096 string a = "" + Directory.GetParent((string) Directory.GetCurrentDirectory());
00097 string b = "" + Directory.GetParent(a);
00098 string c = "" + Directory.GetParent(b);
00099
00100 this.fileName = c + "\\"+_fileName;
00101
00102 if (string.IsNullOrEmpty(fileName))
00103 {
00104 throw new ArgumentNullException(fileName);
00105 }
00106
00107 if (!File.Exists(fileName))
00108 {
00109 Console.WriteLine(fileName);
00110 throw new ArgumentException("file not found: "+ fileName);
00111 }
00112
00113 }
00114 #endregion
00115
00116 #region Initialize()
00117
00118
00124 public override void Initialize()
00125 {
00126 base.Initialize();
00127 this.LoadContent();
00128 }
00129
00130 #endregion
00131
00132 #region LoadContent
00133
00134
00135
00136
00137
00138 protected override void LoadContent()
00139 {
00140 device = Game.GraphicsDevice;
00141 PresentationParameters param = Game.GraphicsDevice.PresentationParameters;
00142 SurfaceFormat format = param.BackBufferFormat;
00143 int width = device.PresentationParameters.BackBufferWidth;
00144 int height = device.PresentationParameters.BackBufferHeight;
00145
00146 frontTarget = new RenderTarget2D(device, width, height, 1, format,
00147 device.DepthStencilBuffer.MultiSampleType,
00148 device.PresentationParameters.MultiSampleQuality);
00149
00150 backTarget = new RenderTarget2D(device, width, height, 1, format,
00151 device.DepthStencilBuffer.MultiSampleType,
00152 device.PresentationParameters.MultiSampleQuality);
00153
00154 screenShotTarget = new RenderTarget2D(device, width, height, 1, format,
00155 device.DepthStencilBuffer.MultiSampleType,
00156 device.PresentationParameters.MultiSampleQuality);
00157
00158
00159
00160 #region check format
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 format = SurfaceFormat.Vector4;
00177 #endregion
00178
00179 if (Game.GraphicsDevice.GraphicsDeviceCapabilities.TextureCapabilities.RequiresPower2)
00180 {
00181 loadDatafromFileExtend();
00182 }
00183 else
00184 {
00185 loadDatafromFile();
00186 }
00187
00188
00189
00190 float max = (float)Math.Max(sizeX, Math.Max(sizeY, sizeZ));
00191 stepSize = Vector3.One/max;
00192 Console.WriteLine(stepSize);
00193
00194 Matrix rotate;
00195 if(GameProperties.Instance.dataFilename.StartsWith("skewed_head"));
00196 {
00197 rotate = Matrix.CreateRotationZ(90.0f);
00198 }
00199 worldMatrix = Matrix.CreateTranslation(new Vector3(-(sizeX / max) / 2.0f,
00200 -(sizeY / max) / 2.0f,
00201 -(sizeZ / max) / 2.0f));
00202
00203
00204
00205
00206
00207 iterations = (int)(max * (1.0f / scaleSteps));
00208
00209
00210 Vector3 sizeVector = new Vector3(sizeX, sizeY, sizeZ);
00211 Vector3 scale = Vector3.One / ( (Vector3.One * max)/sizeVector );
00212 scaleFactor = new Vector4(scale.X, scale.Y, scale.Z, 1.0f);
00213
00214
00215 cube = Game.Content.Load<Model>("Models/box");
00216
00217 shader = new VolumeShader(Game, device, Game.Content.Load<Effect>("Effects\\volumeShader"));
00218 shader.SetEffectParameter(volumeData, stepSize * scaleSteps, iterations, 0, scaleFactor, slice);
00219
00220
00221 colorControlPoints = GameProperties.Instance.colorControlPoints;
00222 alphaControlPoints = GameProperties.Instance.alphaControlPoints;
00223
00224
00225 setSliceRange(sizeX, sizeY, sizeZ);
00226 base.LoadContent();
00227 }
00228
00238 private void loadDatafromFile()
00239 {
00240 #region read data
00241 Stream file = new FileStream(fileName, FileMode.Open);
00242
00243 BinaryReader reader = new BinaryReader(file);
00244
00245
00246 sizeX = reader.ReadUInt16();
00247 sizeY = reader.ReadUInt16();
00248 sizeZ = reader.ReadUInt16();
00249
00250
00251
00252 volumeData = new Texture3D(Game.GraphicsDevice, sizeX, sizeY, sizeZ, 0,
00253 TextureUsage.Linear, SurfaceFormat.Vector4);
00254
00255 ushort[] buffer = new ushort[sizeX * sizeY * sizeZ];
00256
00257 int j = 0;
00258 while (reader.BaseStream.Position < reader.BaseStream.Length)
00259 {
00260
00261 buffer[j] = reader.ReadUInt16();
00262 j++;
00263 }
00264 reader.Close();
00265 #endregion
00266
00267 #region scale data
00268
00269 densityValues = new List<float>();
00270 float maxValue = 1.0f/buffer.Max();
00271 for (int i = 0; i < buffer.Length; i++)
00272 {
00273 densityValues.Add((float)buffer[i] * maxValue);
00274 }
00275 #endregion
00276
00277 GameProperties.Instance.dataOrigin = new Vector3(0.5f, 0.5f, sizeZ / 2.0f);
00278
00279 #region add gradients to VolumeData
00280 calculateGradients();
00281
00282
00283 Vector4[] densityVectors = new Vector4[buffer.Length];
00284 for (int i = 0; i < buffer.Length; i++)
00285 {
00286 densityVectors[i] = new Vector4(gradients[i].X,
00287 gradients[i].Y,
00288 gradients[i].Z,
00289 densityValues[i]);
00290 }
00291 #endregion
00292
00293 densityArray = densityValues.ToArray();
00294
00295
00296 volumeData.SetData<Vector4>(densityVectors);
00297
00298 #region sort data
00299 densityValues.Sort();
00300 sortDensityValues();
00301
00302
00303
00304
00305
00306 #endregion
00307 }
00308
00309
00316 private void sortDensityValues()
00317 {
00318
00319 List<float> distinctDensityValues = new List<float>();
00320 List<int> countDensityValues = new List<int>();
00321
00322 #region debug
00323
00324
00325
00326 #endregion
00327
00328 for (int i = 0; i < densityValues.Count(); i++)
00329 {
00330 int value1 = (int)(densityValues[i] * 255);
00331 int count = 1;
00332
00333 while (i < (densityValues.Count() - 1))
00334 {
00335 int value2 = (int)(densityValues[i + 1] * 255);
00336 if ((value1 == value2))
00337 {
00338 count++;
00339 i++;
00340 }
00341 else
00342 {
00343 break;
00344 }
00345
00346 }
00347
00348 #region debug
00349
00350
00351
00352 #endregion
00353
00354 distinctDensityValues.Add(value1);
00355 countDensityValues.Add(count);
00356
00357 }
00358 distinctDensityValuesArray = distinctDensityValues.ToArray();
00359 countDensityValuesArray = countDensityValues.ToArray();
00360
00361 GameProperties.Instance.distinctDensityValues = distinctDensityValuesArray;
00362 GameProperties.Instance.countDensityValues = countDensityValuesArray;
00363
00364 }
00365
00366 #endregion
00367
00368
00369
00370
00371
00372
00373 public override void Update(GameTime gameTime)
00374 {
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389 Game.GraphicsDevice.RenderState.AlphaBlendEnable = false;
00390 shader.CurrentTechnique = shader.Techniques["RenderPosition"];
00391 shader.SetEffectParameter(worldMatrix*camera.ViewProjection, worldMatrix);
00392 shader.SetEffectParameter(camera.CameraPosition);
00393
00394
00395 Game.GraphicsDevice.SetRenderTarget(0, frontTarget);
00396 Game.GraphicsDevice.Clear(Color.Black);
00397 GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace;
00398
00399 DrawVolume(gameTime, 0);
00400
00401 Game.GraphicsDevice.SetRenderTarget(0, null);
00402
00403
00404 Game.GraphicsDevice.SetRenderTarget(0, backTarget);
00405 Game.GraphicsDevice.Clear(Color.Black);
00406 GraphicsDevice.RenderState.CullMode = CullMode.CullClockwiseFace;
00407
00408 DrawVolume(gameTime, 1);
00409
00410 Game.GraphicsDevice.SetRenderTarget(0, null);
00411 Game.GraphicsDevice.Clear(Color.Black);
00412 shader.FrontFacesTexture = frontTarget.GetTexture();
00413 shader.BackFacesTexture = backTarget.GetTexture();
00414
00415 #region debug
00416
00417
00418
00419
00420
00421
00422 #endregion
00423
00424 setControlpoints();
00425
00426 base.Update(gameTime);
00427 }
00433 public override void Draw(GameTime gameTime)
00434 {
00435 if (currentView == 1)
00436 {
00437 slice = GameProperties.Instance.ySliceValue;
00438 shader.CurrentTechnique = shader.Techniques["RenderSlice"];
00439 }
00440 else if (currentView == 2)
00441 {
00442 slice = GameProperties.Instance.xSliceValue;
00443 shader.CurrentTechnique = shader.Techniques["RenderSlice"];
00444 }
00445 else if (currentView == 3)
00446 {
00447 slice = GameProperties.Instance.zSliceValue;
00448 shader.CurrentTechnique = shader.Techniques["RenderSlice"];
00449 }
00450 else
00451 {
00452
00453 if (GameProperties.Instance.maximumIntensityProjection == true)
00454 {
00455 shader.CurrentTechnique = shader.Techniques["MaximumIntensity"];
00456 }
00457 else
00458 {
00459 shader.CurrentTechnique = shader.Techniques["RayCastSimple"];
00460 }
00461 }
00462
00463
00464
00465
00466
00467
00468 Game.GraphicsDevice.RenderState.AlphaBlendEnable = false;
00469
00470
00471
00472
00473 #region depricated : do we need it!?
00474
00475
00476
00477
00478
00479
00480 #endregion
00481
00482 #region save screenshot
00483 if (GameProperties.Instance.saveView)
00484 {
00485 if (string.IsNullOrEmpty(GameProperties.Instance.saveFilename))
00486 {
00487 throw new ArgumentNullException(GameProperties.Instance.saveFilename);
00488 }
00489 Game.GraphicsDevice.SetRenderTarget(0, screenShotTarget);
00490 Game.GraphicsDevice.Clear(Color.Black);
00491 GraphicsDevice.RenderState.CullMode = CullMode.CullClockwiseFace;
00492
00493 DrawVolume(gameTime, currentView);
00494
00495 Game.GraphicsDevice.SetRenderTarget(0, null);
00496 Texture2D saveTex = screenShotTarget.GetTexture();
00497 saveTex.Save(GameProperties.Instance.saveFilename, ImageFileFormat.Jpg);
00498
00499
00500 GameProperties.Instance.saveView = false;
00501 }
00502 #endregion
00503
00504
00505 GraphicsDevice.RenderState.CullMode = CullMode.None;
00506
00507
00508 DrawVolume(gameTime, currentView);
00509
00510
00511 }
00512
00518 private void DrawVolume(GameTime gameTime, int side)
00519 {
00520 #region draw cube
00521
00522
00523
00524 foreach (ModelMesh mesh in cube.Meshes)
00525 {
00526 foreach (ModelMeshPart mmp in mesh.MeshParts)
00527 {
00528 mmp.Effect = shader;
00529 shader.SetEffectParameter(volumeData, stepSize * scaleSteps, iterations, side, scaleFactor, slice);
00530 shader.SetEffectParameter(camera.CameraPosition);
00531 };
00532
00533
00534 mesh.Draw();
00535
00536 }
00537 #endregion
00538 }
00539
00544 private void DrawCoordinateSystem(GameTime gameTime)
00545 {
00546
00547 #region coordinate system
00548
00549 BasicEffect basicEffect = new BasicEffect(device, null);
00550 device.RenderState.DepthBufferEnable = false;
00551 device.VertexDeclaration = new VertexDeclaration(device, VertexPositionColor.VertexElements);
00552
00553 basicEffect.LightingEnabled = false;
00554 basicEffect.TextureEnabled = false;
00555 basicEffect.VertexColorEnabled = true;
00556 basicEffect.World = Matrix.Identity;
00557 basicEffect.Projection = camera.Projection;
00558 basicEffect.View = camera.View;
00559
00560 basicEffect.Begin();
00561 foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
00562 {
00563 pass.Begin();
00564 VertexPositionColor[] pointList = {
00565
00566 new VertexPositionColor(new Vector3(0.0f, 0.0f, 0.0f), Color.DarkBlue),
00567 new VertexPositionColor(new Vector3(0.0f, 0.0f, -3000.0f), Color.DarkBlue),
00568 new VertexPositionColor(new Vector3(0.0f, 0.0f, 0.0f), Color.Blue),
00569 new VertexPositionColor(new Vector3(0.0f, 0.0f, 3000.0f), Color.Blue),
00570
00571 new VertexPositionColor(new Vector3(0.0f, 0.0f, 0.0f), Color.DarkGreen),
00572 new VertexPositionColor(new Vector3(0.0f, -3000.0f, 0.0f), Color.DarkGreen),
00573 new VertexPositionColor(new Vector3(0.0f, 0.0f, 0.0f), Color.Green),
00574 new VertexPositionColor(new Vector3(0.0f, 3000.0f, 0.0f), Color.Green),
00575
00576 new VertexPositionColor(new Vector3(0.0f, 0.0f, 0.0f), Color.DarkRed),
00577 new VertexPositionColor(new Vector3(-3000.0f, 0.0f, 0.0f), Color.DarkRed),
00578 new VertexPositionColor(new Vector3(0.0f, 0.0f, 0.0f), Color.Red),
00579 new VertexPositionColor(new Vector3(3000.0f, 0.0f, 0.0f), Color.Red),
00580
00581 new VertexPositionColor(new Vector3(10.0f + 2.5f, 5.0f, 0.0f), Color.DarkRed),
00582 new VertexPositionColor(new Vector3(10.0f + 1.25f, 1.25f, 0.0f), Color.DarkRed),
00583 new VertexPositionColor(new Vector3(10.0f + 1.25f, 5.0f, 0.0f), Color.DarkRed),
00584 new VertexPositionColor(new Vector3(10.0f + 2.5f, 1.25f, 0.0f), Color.DarkRed),
00585
00586 new VertexPositionColor(new Vector3(1.875f, 10.0f + 3.125f, 0.0f), Color.DarkGreen),
00587 new VertexPositionColor(new Vector3(1.875f, 10.0f + 1.125f, 0.0f), Color.DarkGreen),
00588 new VertexPositionColor(new Vector3(1.875f, 10.0f + 3.125f, 0.0f), Color.DarkGreen),
00589 new VertexPositionColor(new Vector3(2.5f, 10.0f + 5.0f, 0.0f), Color.DarkGreen),
00590 new VertexPositionColor(new Vector3(1.875f, 10.0f + 3.125f, 0.0f), Color.DarkGreen),
00591 new VertexPositionColor(new Vector3(1.125f, 10 + 5.0f, 0.0f), Color.DarkGreen),
00592
00593 new VertexPositionColor(new Vector3(0.0f, 5.0f, 10.0f + 2.5f), Color.DarkBlue),
00594 new VertexPositionColor(new Vector3(0.0f, 5.0f, 10.0f + 1.25f), Color.DarkBlue),
00595 new VertexPositionColor(new Vector3(0.0f, 1.25f, 10.0f + 2.5f), Color.DarkBlue),
00596 new VertexPositionColor(new Vector3(0.0f, 1.25f, 10.0f + 1.25f), Color.DarkBlue),
00597 new VertexPositionColor(new Vector3(0.0f, 1.25f, 10.0f + 2.5f), Color.DarkBlue),
00598 new VertexPositionColor(new Vector3(0.0f, 5.0f, 10.0f + 1.25f), Color.DarkBlue)
00599
00600 };
00601 short[] lineListIndices = new short[28] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27 };
00602 Game.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
00603 PrimitiveType.LineList,
00604 pointList,
00605 0,
00606 28,
00607 lineListIndices,
00608 0,
00609 14
00610 );
00611 pass.End();
00612 }
00613 basicEffect.End();
00614 device.RenderState.DepthBufferEnable = true;
00615 #endregion
00616 }
00617
00618 #region transfer function
00619
00620
00621
00622
00623 private void setControlpoints()
00624 {
00625 colorControlPoints = GameProperties.Instance.colorControlPoints;
00626 alphaControlPoints = GameProperties.Instance.alphaControlPoints;
00627
00628 #region debug
00629
00630
00631 #endregion
00632
00633 float amount = 0.0f;
00634 Color[] colorArray = new Color[256];
00635 float[] alphaArray = new float[256];
00636
00637 #region interpolate between ControlPoints
00638
00639
00640 colorControlPoints.Sort(delegate(TransferControlPoint p1,
00641 TransferControlPoint p2) { return p1.isoValue.CompareTo(p2.isoValue); });
00642 alphaControlPoints.Sort(delegate(TransferControlPoint p1,
00643 TransferControlPoint p2) { return p1.isoValue.CompareTo(p2.isoValue); });
00644
00645 Console.WriteLine(alphaControlPoints[1].color.W);
00646
00647
00648
00649
00650
00651
00652 for (int i = 0; i < colorControlPoints[0].isoValue; i++)
00653 {
00654 colorArray[i] = Color.Black;
00655 alphaArray[i] = 0.0f;
00656 }
00657
00658 for (int j = 0; j < colorControlPoints.Count-1; j++)
00659 {
00660 for (int i = colorControlPoints[j].isoValue; i < colorControlPoints[j+1].isoValue; i++)
00661 {
00662 amount = (((float)i - colorControlPoints[j].isoValue) /
00663 (colorControlPoints[j+1].isoValue - colorControlPoints[j].isoValue));
00664 colorArray[i] = Color.Lerp(new Color(colorControlPoints[j].color * 255),
00665 new Color(colorControlPoints[j+1].color * 255), amount);
00666
00667
00668 alphaArray[i] = alphaControlPoints[j].color.W * (1.0f - amount) +
00669 alphaControlPoints[j + 1].color.W * amount;
00670 }
00671 }
00672
00673
00674 for (int i = colorControlPoints[colorControlPoints.Count-1].isoValue+1; i < 256; i++)
00675 {
00676 colorArray[i] = Color.Black;
00677 alphaArray[i] = 0.0f;
00678 }
00679
00680 GameProperties.Instance.colorArray = colorArray;
00681 GameProperties.Instance.alphaArray = alphaArray;
00682 GameProperties.Instance.updateColorBar = true;
00683
00684 #endregion
00685
00686 #region write to textures
00687
00688 transferTexture = new Texture2D(Game.GraphicsDevice, 256, 1, 1, TextureUsage.Linear, SurfaceFormat.Color);
00689 transferTextureAlpha = new Texture2D(Game.GraphicsDevice, 256, 1, 1, TextureUsage.Linear, SurfaceFormat.Color);
00690
00691
00692 Byte4[] transferValues = new Byte4[256];
00693 Byte4[] alphaValues = new Byte4[256];
00694 for (int i = 0; i < 256; i++)
00695 {
00696
00697 Vector4 color = new Vector4(colorArray[i].R, colorArray[i].G, colorArray[i].B, alphaArray[i]);
00698 color *= 255.0f;
00699
00700 transferValues[i] = new Byte4(color.Z, color.Y, color.X, color.W);
00701 Vector4 alpha = new Vector4(alphaArray[i]*255.0f);
00702
00703 if ((i < 5) || (i > 250))
00704 {
00705 alpha = new Vector4(255.0f, 0.0f, 0.0f, 1.0f);
00706 }
00707 alphaValues[i] = new Byte4(alpha.Z, alpha.Y, alpha.X, alpha.W);
00708 }
00709
00710 transferTexture.SetData(transferValues);
00711 transferTextureAlpha.SetData(alphaValues);
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723 #endregion
00724
00725 shader.TransferTexture = this.transferTexture;
00726 shader.TransferTextureAlpha = this.transferTextureAlpha;
00727
00728
00729
00730
00731
00732 }
00733
00734 #endregion
00735
00736 #region Gradients
00737
00745 private float getSampleAt(int x, int y, int z)
00746 {
00747 x = (int)MathHelper.Clamp(x, 0, sizeX - 1);
00748 y = (int)MathHelper.Clamp(y, 0, sizeY - 1);
00749 z = (int)MathHelper.Clamp(z, 0, sizeZ - 1);
00750
00751
00752
00753 int arrayIndex = x + y * sizeX + z * sizeX * sizeY;
00754
00755
00756
00757
00758
00759 return densityValues[arrayIndex];
00760
00761
00762 }
00763
00771 private void calculateGradients()
00772 {
00773
00774
00775
00776
00777
00778
00779 int sampleSize = GameProperties.Instance.gradientSampleSize;
00780
00781 gradients = new Vector3[sizeX * sizeY * sizeZ];
00782
00783 Vector3 normal = Vector3.Zero;
00784
00785 int i = 0;
00786 for (int z = 0; z < sizeZ; z++)
00787 {
00788 for (int y = 0; y < sizeY; y++)
00789 {
00790 for (int x = 0; x < sizeX; x++)
00791 {
00792 Vector3 gradient = new Vector3(
00793 getSampleAt(x + sampleSize, y, z) - getSampleAt(x - sampleSize, y, z),
00794 getSampleAt(x, y + sampleSize, z) - getSampleAt(x, y - sampleSize, z),
00795 getSampleAt(x, y, z + sampleSize) - getSampleAt(x, y, z - sampleSize));
00796
00797
00798
00799 gradients[i] = gradient;
00800 i++;
00801 }
00802 }
00803 }
00804 }
00805
00806 #endregion
00807
00808 #region help functions
00809
00810
00811
00812
00813 private void loadDatafromFileExtend()
00814 {
00815 #region read data
00816 Stream file = new FileStream(fileName, FileMode.Open);
00817
00818 BinaryReader reader = new BinaryReader(file);
00819
00820
00821 sizeX = reader.ReadUInt16();
00822 sizeY = reader.ReadUInt16();
00823 sizeZ = reader.ReadUInt16();
00824 ushort sizeXOld = sizeX;
00825 ushort sizeYOld = sizeY;
00826 ushort sizeZOld = sizeZ;
00827
00828 sizeX = calculatePowerOfTwo(sizeX);
00829 sizeY = calculatePowerOfTwo(sizeY);
00830 sizeZ = calculatePowerOfTwo(sizeZ);
00831
00832
00833
00834 List<float> scalars = new List<float>();
00835
00836 volumeData = new Texture3D(Game.GraphicsDevice, sizeX, sizeY, sizeZ, 0,
00837 TextureUsage.Linear, SurfaceFormat.Vector4);
00838 for (int idxZ = 0; idxZ < sizeZ; idxZ++)
00839 {
00840 for (int idxY = 0; idxY < sizeY; idxY++)
00841 {
00842 for (int idxX = 0; idxX < sizeX; idxX++)
00843 {
00844 if (idxX >= sizeXOld || idxY >= sizeYOld || idxZ >= sizeZOld)
00845 {
00846 scalars.Add(0.0f);
00847
00848 }
00849 else
00850 {
00851 ushort value = reader.ReadUInt16();
00852 scalars.Add((float)value);
00853
00854 }
00855 }
00856 }
00857 }
00858
00859 reader.Close();
00860 #endregion
00861
00862 #region scale data
00863 float maxVal = 1.0f / scalars.Max();
00864 for (int i = 0; i < scalars.Count(); i++)
00865 {
00866 scalars[i] = scalars[i] * maxVal;
00867 }
00868 #endregion
00869
00870 #region add gradients to VolumeData
00871 densityValues = scalars;
00872
00873 calculateGradients();
00874 Vector4[] densityVectors = new Vector4[scalars.Count()];
00875 for (int i = 0; i < scalars.Count(); i++)
00876 {
00877 densityVectors[i] = new Vector4(gradients[i].X,
00878 gradients[i].Y,
00879 gradients[i].Z,
00880 densityValues[i]);
00881 }
00882 volumeData.SetData<Vector4>(densityVectors);
00883 #endregion
00884
00885 #region sort data
00886 densityValues.Sort();
00887 sortDensityValues();
00888 #endregion
00889
00890 }
00891
00898 private ushort calculatePowerOfTwo(int value)
00899 {
00900
00901 if (value > 0)
00902 {
00903 if ((value & (value - 1)) == 0) return (ushort)value;
00904
00905
00906 else
00907 {
00908 value--;
00909 for (int i = 1; i < sizeof(int) * 8; i <<= 1)
00910 {
00911 value = value | value >> 1;
00912 }
00913
00914 ushort returnValue = (ushort)(value + 1);
00915 return returnValue;
00916 }
00917 }
00918 else
00919 {
00920 return 1;
00921 }
00922 }
00923
00924 private void setSliceRange(int xRange, int yRange, int zRange)
00925 {
00926 GameProperties.Instance.xDataRange = xRange;
00927 GameProperties.Instance.yDataRange = yRange;
00928 GameProperties.Instance.zDataRange = zRange;
00929 GameProperties.Instance.updateGuiForm = true;
00930 }
00931 #endregion
00932 }
00933 }