1 #region Using Statement
4 using System.Windows.Forms;
5 using System.Collections.Generic;
8 using Microsoft.Xna.Framework;
9 using Microsoft.Xna.Framework.Content;
10 using Microsoft.Xna.Framework.Graphics;
14 using Color = Microsoft.Xna.Framework.Color;
15 using Rectangle = Microsoft.Xna.Framework.Rectangle;
16 using Point = System.Drawing.Point;
28 SortedDictionary<int, TransferControlPoint> controlPointValues;
29 SortedDictionary<float, float> densityIsoValues;
30 bool enableTransferFunction;
32 VertexPositionColor[] densityPoints;
33 VertexPositionColor[] controlPoints;
39 bool drawDensityFunction;
44 SpriteBatch spriteBatch;
45 ContentManager content;
46 Texture2D transparentPattern;
47 Texture2D tfBackground;
48 Texture2D controlPointTex;
49 Color[] tfBackgroundColor;
53 bool getControlPoints;
54 bool setControlPoints;
57 Size defaultSize =
new Size(256,256);
63 public bool EnableTransferFunction
65 set { enableTransferFunction = value; }
67 public bool DensityFunction
69 set { drawDensityFunction = value; }
72 public bool UpdateIsoValues
74 set { updateIsoValues = value; }
77 public bool GetControlPoints
79 set { getControlPoints = value; }
82 public bool SetControlPoints
84 set { setControlPoints = value; }
85 get {
return setControlPoints; }
89 protected override void Initialize()
95 spriteBatch = Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
96 content = Services.GetService(typeof(ContentManager)) as ContentManager;
98 updateIsoValues =
false;
99 getControlPoints =
false;
100 setControlPoints =
false;
103 controlPointValues =
new SortedDictionary<int, TransferControlPoint>();
104 getControlPointsData();
106 tfBackgroundColor =
new Color[256*256];
108 densityIsoValues =
new SortedDictionary<float, float>();
110 enableTransferFunction =
GameProperties.Instance.enableTransferFunction;
114 protected override void LoadContent()
117 if (spriteBatch == null)
120 spriteBatch =
new SpriteBatch(GraphicsDevice);
121 Services.AddService<SpriteBatch>(spriteBatch);
127 content =
new ContentManager(Services,
"Content");
128 Services.AddService<ContentManager>(content);
131 transparentPattern = content.Load<Texture2D>(
"Texture\\transparent_pattern");
132 tfBackground =
new Texture2D(GraphicsDevice, 256, 256,
false, SurfaceFormat.Color);
134 tfBackground.SetData(tfBackgroundColor);
136 controlPointTex = content.Load<Texture2D>(
"Texture\\circle");
139 effect =
new BasicEffect(GraphicsDevice);
140 effect.VertexColorEnabled =
true;
149 Console.WriteLine(
"Transferfunction Update Call");
151 if (!enableTransferFunction)
return;
153 #region update iso values
157 updateIsoValuesData();
159 updateIsoValues =
false;
164 #region update control points
168 if (getControlPoints)
170 getControlPointsData();
173 tfBackground.SetData(tfBackgroundColor);
174 getControlPoints =
false;
178 if (setControlPoints)
182 tfBackground.SetData(tfBackgroundColor);
183 setControlPoints =
false;
187 #region control size changed
192 if (!getControlPoints && !setControlPoints)
196 if (drawDensityFunction)
210 Console.WriteLine(
"Transferfunction Draw Call");
212 if (!enableTransferFunction)
214 GraphicsDevice.Clear(Color.CornflowerBlue);
215 #region draw transparent pattern texture as background
216 spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);
217 spriteBatch.Draw(transparentPattern,
new Rectangle(0, 0, Width, Height), Color.White);
223 GraphicsDevice.Clear(Color.CornflowerBlue);
224 effect.World = Matrix.Identity;
225 effect.View = Matrix.CreateLookAt(
new Vector3(0.0f, 0.0f, 1.0f), Vector3.Zero, Vector3.Up);
226 effect.Projection = Matrix.CreateOrthographicOffCenter(0, (
float)GraphicsDevice.Viewport.Width, (
float)GraphicsDevice.Viewport.Height, 0, 1.0f, 1000.0f);
229 #region draw background
230 spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);
231 spriteBatch.Draw(transparentPattern,
new Rectangle(0, 0, Width, Height), Color.White);
232 spriteBatch.Draw(tfBackground,
new Rectangle(0, 0, Width, Height), Color.White);
237 #region draw density function
238 if (drawDensityFunction)
240 GraphicsDevice.RasterizerState = RasterizerState.CullNone;
241 effect.CurrentTechnique.Passes[0].Apply();
242 GraphicsDevice.DrawUserPrimitives(PrimitiveType.LineStrip,
243 densityPoints, 0, (densityPoints.Count()-1));
248 #region draw control points
251 GraphicsDevice.RasterizerState = RasterizerState.CullNone;
252 effect.CurrentTechnique.Passes[0].Apply();
253 GraphicsDevice.DrawUserPrimitives(PrimitiveType.LineStrip,
254 controlPoints, 0, (controlPoints.Count() - 1));
256 spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);
257 foreach (KeyValuePair<int,TransferControlPoint> p
in controlPointValues)
260 float scaleX_ = ((float)(this.Width - 1) / (float)scaleX);
261 float scaleY_ = ((float)(this.Height - 1) / (float)scaleY);
264 int x = (int)(p.Value.isoValue * scaleX_ - (
int)(cpTexWidth * 0.5f));
265 int y = (int)((this.Height - 1 - p.Value.color.W * 255.0f * scaleY_) - (int)(cpTexHeight * 0.5f));
266 Color color =
new Color(p.Value.color);
268 spriteBatch.Draw(controlPointTex,
new Rectangle(x, y, cpTexHeight, cpTexWidth), color);
274 #region Help Functions
279 private void getControlPointsData()
285 controlPointValues.Clear();
286 for (
int i = 0; i <
GameProperties.Instance.colorControlPoints.Count(); i++)
288 int cpValue =
GameProperties.Instance.colorControlPoints[i].isoValue;
290 cpNext.color.W =
GameProperties.Instance.alphaControlPoints[i].color.W;
291 controlPointValues.Add(cpValue, cpNext);
297 if (controlPointValues.Count > 0) controlPointValues.Clear();
300 #region old version -> delete after test
331 if (controlPointValues == null || controlPointValues.Count == 0)
return;
333 controlPoints =
new VertexPositionColor[controlPointValues.Count];
334 float scaleX_ = ((float)(this.Width - 1) / (float)scaleX);
335 float scaleY_ = ((float)(this.Height - 1) / (float)scaleY);
337 foreach (KeyValuePair<int, TransferControlPoint> cp
in controlPointValues)
340 Color color =
new Color(cp.Value.color);
342 controlPoints[i] =
new VertexPositionColor(
343 new Vector3((cp.Value.isoValue * scaleX_), (
this.Height - 1) - ((cp.Value.color.W * 255.0f) * scaleY_), 0),
359 void interpolateColor()
362 #region current version
364 List<TransferControlPoint> cpList;
366 if (GameProperties.Instance.colorControlPoints != null && GameProperties.Instance.colorControlPoints.Count > 0)
368 cpList =
new List<TransferControlPoint>(GameProperties.Instance.colorControlPoints);
373 cpList =
new List<TransferControlPoint>();
374 foreach(KeyValuePair<int, TransferControlPoint> cp
in controlPointValues)
377 cpList.Add(cp.Value);
382 cpList.Sort(delegate(TransferControlPoint p1,
383 TransferControlPoint p2) {
return p1.isoValue.CompareTo(p2.isoValue); });
388 for (
int k = 0; k < 256; k++)
390 byte alpha = (byte)(255 - k);
392 for (
int i = 0; i < cpList[0].isoValue; i++)
394 tfBackgroundColor[i + 256 * k] = Color.Black;
395 tfBackgroundColor[i + 256 * k].A = alpha;
399 for (
int j = 0; j < cpList.Count - 1; j++)
401 for (
int i = cpList[j].isoValue; i <= cpList[j + 1].isoValue; i++)
403 amount = (((float)i - cpList[j].isoValue) /
404 (cpList[j + 1].isoValue - cpList[j].isoValue));
405 tfBackgroundColor[i + 256 * k] = Color.Lerp(
new Color(cpList[j].color),
406 new Color(cpList[j + 1].color), amount);
407 tfBackgroundColor[i + 256 * k].A = alpha;
412 for (
int i = cpList[cpList.Count - 1].isoValue + 1; i < 256; i++)
414 tfBackgroundColor[i + 256 * k] = Color.Black;
415 tfBackgroundColor[i + 256 * k].A = alpha;
421 #region old version 2
473 private void updateIsoValuesData()
475 if (GameProperties.Instance.distinctDensityValues != null && GameProperties.Instance.distinctDensityValues.Length > 0)
477 densityIsoValues.Clear(); ;
479 float maxDensity = (float)Math.Log10(GameProperties.Instance.countDensityValues.Max() - GameProperties.Instance.countDensityValues.Min());
480 for (
int i = 0; i < GameProperties.Instance.distinctDensityValues.Length; i++)
482 densityIsoValues.Add(GameProperties.Instance.distinctDensityValues[i], (
float)Math.Log10(GameProperties.Instance.countDensityValues[i])/maxDensity);
492 void setDensityPoints()
494 if (densityIsoValues == null || densityIsoValues.Count == 0)
return;
495 densityPoints =
new VertexPositionColor[densityIsoValues.Count];
497 float scaleX_ = ((float)(this.Width - 1) / (float)scaleX);
498 float scaleY_ = ((float)(this.Height - 1) / (float)scaleY);
500 foreach (KeyValuePair<float, float> isoValue
in densityIsoValues)
502 densityPoints[i] =
new VertexPositionColor(
new Vector3(isoValue.Key * scaleX_, ((
this.Height - 1) - (isoValue.Value*255.0f)*scaleY_), 0), Color.Black);
509 #region Control Points Helper Functions
510 public void setControlPoint(Point position, System.Drawing.Color color)
519 float r = (float)(color.R / 255.0f);
520 float g = (float)(color.G / 255.0f);
521 float b = (float)(color.B / 255.0f);
522 float a = (float)((255 - position.Y) / 255.0f);
524 int key = getControlPointKey(position);
528 float scaleX_ = ((float)(this.Width - 1) / (float)scaleX);
529 int posX = (int)(position.X / scaleX_);
530 GameProperties.Instance.colorControlPoints.Add(
new TransferControlPoint(r, g, b, posX));
531 GameProperties.Instance.alphaControlPoints.Add(
new TransferControlPoint(a, posX));
532 TransferControlPoint cpNew =
new TransferControlPoint(r, g, b, posX);
534 controlPointValues.Add(position.X, cpNew);
539 controlPointValues[key].color.X = r;
540 controlPointValues[key].color.Y = g;
541 controlPointValues[key].color.Z = b;
542 controlPointValues[key].color.W = a;
544 for (
int i = 0; i < GameProperties.Instance.colorControlPoints.Count(); i++)
546 if (GameProperties.Instance.colorControlPoints[i].isoValue == key
547 && GameProperties.Instance.alphaControlPoints[i].isoValue == key)
549 GameProperties.Instance.colorControlPoints[i].color.X = r;
550 GameProperties.Instance.colorControlPoints[i].color.Y = g;
551 GameProperties.Instance.colorControlPoints[i].color.Z = b;
552 GameProperties.Instance.alphaControlPoints[i].color.W = a;
553 i = GameProperties.Instance.colorControlPoints.Count();
571 int key = getControlPointKey(oldPosition);
577 float scaleX_ = ((float)(this.Width - 1) / (float)scaleX);
578 float scaleY_ = ((float)(this.Height - 1) / (float)scaleY);
581 int newIsoValue = (int)((newPosition.X) / scaleX_);
582 float newAlphaValue = (float)((this.Height - 1 - newPosition.Y) / scaleY_);
583 newAlphaValue /= 255.0f;
586 if (newIsoValue == oldPosition.X)
588 controlPointValues[newIsoValue].color.W = newAlphaValue;
593 cp.isoValue = newIsoValue;
594 cp.color.W = newAlphaValue;
595 controlPointValues.Remove(oldPosition.X);
596 if (controlPointValues.ContainsKey(newIsoValue))
597 controlPointValues[newIsoValue] = cp;
598 else controlPointValues.Add(newIsoValue, cp);
602 changeControlPoints();
615 int key = getControlPointKey(cpPosition);
618 if (controlPointValues.Remove(key))
620 for (
int i = 0; i <
GameProperties.Instance.colorControlPoints.Count(); i++)
641 void changeControlPoints()
646 foreach (KeyValuePair<int, TransferControlPoint> cp
in this.controlPointValues)
659 int getControlPointKey(Point mousePos)
662 float scaleX_ = ((float)(this.Width - 1) / (float)scaleX);
663 float scaleY_ = ((float)(this.Height - 1) / (float)scaleY);
665 mousePos.X = (int)(mousePos.X / scaleX_);
666 mousePos.Y = (int)((this.Height - 1 - mousePos.Y) / scaleY_);
668 if (controlPointValues.ContainsKey(mousePos.X))
return mousePos.X;
673 foreach (
int key in controlPointValues.Keys)
675 if (mousePos.X >= (key - (cpTexWidth / 2)) &&
676 mousePos.X <= (key + (cpTexWidth / 2)))
687 #region Control Properties Events
688 protected override void OnSizeChanged(EventArgs e)
691 if (this.Size != lastSize)
694 lastSize = this.Size;
698 base.OnSizeChanged(e);