image
Add references: Kitware.mymmy.Runtime.dll and KitwareVTK.dll==============================================using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kitware.VTK;
namespace SimplePointsReader
{
public partial class Form1 : Form
{
private Kitware.VTK.RenderWindowControl renderWindowControl1;
public Form1()
{
InitializeComponent();
renderWindowControl1 = new RenderWindowControl();
this.Controls.Add(renderWindowControl1);
renderWindowControl1.Show();
renderWindowControl1.Dock = DockStyle.Fill;
}

me1237guy 發表在 痞客邦 留言(0) 人氣()

image
p0離紅色線段最近的點,即本身所在位置(1,0,0), 所以距離平方為0p1離紅色線段最近的點(1, 0, 0), 距離平方為4// DistancePointToLine.cpp : 定義主控台應用程式的進入點。
//
#include "stdafx.h"
#include <vtkSmartPointer.h>
#include <vtkLine.h>
#include <vtkPoints.h>
int _tmain(int argc, _TCHAR* argv[])
{
double lineP0[3] = {0.0, 0.0, 0.0};
double lineP1[3] = {1.0, 0.0, 0.0};
double p0[3] = {1.0, 0, 0};
double p1[3] ={ 1.0, 2.0, 0};
/*
vtkSmartPointer<vtkLine> line =
vtkSmartPointer<vtkLine>::New();
line->GetPoints()->SetPoint(0, lineP0);
line->GetPoints()->SetPoint(0, lineP1);
*/
{
double dist0 = vtkLine::DistanceToLine(p0, lineP0, lineP1);
std::cout << "Dist0: " << dist0 << std::endl;
double dist1 = vtkLine::DistanceToLine(p1, lineP0, lineP1);
std::cout << "Dist1: " << dist1 << std::endl;
}
{
double t;
double closest[3];
double dist0 = vtkLine::DistanceToLine(p0, lineP0, lineP1, t, closest);
std::cout << "Dist0: " << dist0 << " closest point: " << closest[0] << " " << closest[1] << " " << closest[2] << std::endl;
double dist1 = vtkLine::DistanceToLine(p1, lineP0, lineP1, t, closest);
std::cout << "Dist1: " << dist1 << " closest point: " << closest[0] << " " << closest[1] << " " << closest[2] << std::endl;
}
char c;
std::cin >> c;
return 0;
}

me1237guy 發表在 痞客邦 留言(0) 人氣()

image
同步兩個執行緒, 當Counter1++完成, 換Counter2++, 利用AutoResetEvent機制交換控制權由於同步的關係, 儘管兩組計數器跳動很快, 但下列條件成立 |Counter1 -Counter2| <= 1public class Worker
{
int m_cnt1 = 0, m_cnt2 = 0;
Thread m_thread1;
Thread m_thread2;
TextBox m_textBox1, m_textBox2;
readonly AutoResetEvent m_syncEvent1 = new AutoResetEvent(false);
readonly AutoResetEvent m_syncEvent2 = new AutoResetEvent(true);
ManualResetEvent m_pauseEvent = new ManualResetEvent(true);
ManualResetEvent m_shutdownEvent = new ManualResetEvent(false);
void Job1()
{
while (true)
{
m_pauseEvent.WaitOne(Timeout.Infinite);
if (m_shutdownEvent.WaitOne(0))
break;
m_syncEvent2.WaitOne();
m_cnt1++;
m_textBox1.BeginInvoke((MethodInvoker)delegate {
m_textBox1.Text = m_cnt1.ToString();
});
Thread.Sleep(1);
m_syncEvent1.Set();
}
}
void Job2()
{
while (true)
{
m_pauseEvent.WaitOne(Timeout.Infinite);
if (m_shutdownEvent.WaitOne(0))
break;
m_syncEvent1.WaitOne();
m_cnt2++;
m_textBox2.BeginInvoke((MethodInvoker)delegate
{
m_textBox2.Text = m_cnt2.ToString();
});
Thread.Sleep(1);
m_syncEvent2.Set();
}
}
public Worker() { }
public Worker(TextBox textBox1, TextBox textBox2)
{
m_textBox1 = textBox1;
m_textBox2 = textBox2;
}
public void Start()
{
m_thread1 = new Thread(new ThreadStart(Job1));
m_thread2 = new Thread(new ThreadStart(Job2));
m_thread1.Start();
m_thread2.Start();
}
public void Pause()
{
m_pauseEvent.Reset();
}
public void Resume()
{
m_pauseEvent.Set();
}
public void Stop()
{
m_shutdownEvent.Set();
m_pauseEvent.Set();
m_thread1.Join();
m_thread2.Join();
m_thread1 = null;
m_thread2 = null;
}
}

me1237guy 發表在 痞客邦 留言(0) 人氣()


參考資料[C#]Dictionary的foreach使用KeyValuePair
C# Dictionary的用法,以及查字典的用途

C#Hashtable與Dictionary比較性能

雜湊(Hash)C# Dictionary用法 recordHow do I read and write a C# string Dictionary to a file?

me1237guy 發表在 痞客邦 留言(0) 人氣()

image
Worker類別public class Worker
{
/* Initializes a new intance of the ManualRestEvent class
* with a Boolean value indicating whether to set the
* initial state signaled
*/
private ManualResetEvent m_pauseEvent = new ManualResetEvent(true);
private ManualResetEvent m_shutdownEvent = new ManualResetEvent(false);
private Thread m_thread;
private int m_cnt = 0;
private string m_status;
private Label m_lbl;
private TextBox m_tBox;
private ProgressBar m_pBar;
public int UserID { get; set; }
public string Status
{
get { return m_status; }
}
public int Count
{
get { return m_cnt; }
set { m_cnt = value; }
}
public Worker() { }
public Worker(Label lbl, TextBox tBox, ProgressBar pBar)
{
m_lbl = lbl;
m_tBox = tBox;
m_pBar = pBar;
}
#region Job
public void Job()
{
const int UPDATE_PERIOD = 10000;
while (true)
{
/* 若收到訊號, 則m_pauseEvent程式繼續往下執行
* 反之, 沒有收到訊號, 則持續等待(infinite),
* 程式不會往下執行(凍結)
*/
m_pauseEvent.WaitOne(Timeout.Infinite);
/* 若沒有收到訊號, 則一直等待訊號,
* 但因為timeout設定等待 0 ms, 自動跳開回傳false
* 反之, 收到訊號, 回傳true
*/
if(m_shutdownEvent.WaitOne(0))
break;
m_cnt++;
if (m_cnt % UPDATE_PERIOD == 0)
{
updateTextBox();
updateProgressBar();
}
}
}
#endregion
#region Start
public void Start()
{
m_thread = new Thread(Job);
m_thread.Start();
m_status = "執行緒開始執行...";
updateLabel();
}
#endregion
#region Pause
public void Pause()
{
// 將事件設定為未收到訊號, 會造成執行緒封鎖
m_pauseEvent.Reset();
m_status = "執行緒暫停";
updateLabel();
}
#endregion
#region Resume
public void Resume()
{
// 將事件設定為收到訊號, 會封鎖的執行緒繼續
m_pauseEvent.Set();
m_status = "執行緒繼續";
updateLabel();
}
#endregion
#region Stop
public void Stop()
{
/* m_shutdownEven事件收到訊號,
* 則(m_shutdownEvent.WaitOne(0))
* 回傳true
*/
m_shutdownEvent.Set();
/* 記得通知_pauseEvent, 即_pauseEvent.Set();
* 否則通知_shutdownEvent也枉然
*/
m_pauseEvent.Set();
/* 優雅地等執行緒結束,
* 即 呼叫_thread.Join();
* 而不是Thread.Terminate
*/
m_thread.Join();
m_status = "執行緒停止";
updateLabel();
}
#endregion
#region Reset Cnt
public void ResetCnt()
{
m_cnt = 0;
}
#endregion
#region Update UI
public void updateLabel()
{
if (m_lbl.InvokeRequired)
{
m_lbl.BeginInvoke((Action)(() => { updateLabel(); }));
}
else
{
m_lbl.Text = m_status;
}
}
public void updateTextBox()
{
if (m_tBox.InvokeRequired)
{
m_tBox.BeginInvoke((Action)(() => { updateTextBox(); }));
}
else
{
m_tBox.Text = m_cnt.ToString();
}
}
public void updateProgressBar()
{
if (m_pBar.InvokeRequired)
{
m_pBar.BeginInvoke((Action) (()=>{ updateProgressBar();}) );
}
else
{
m_pBar.Value = m_cnt;
}
}
#endregion
}

me1237guy 發表在 痞客邦 留言(0) 人氣()

image
今天來複習一下執行緒, 先前有一些觀念錯誤: 關於暫停/關閉執行緒Suspend, Terminate不該踩到地雷, 我一個都沒錯過@@ 1. 本範例示範如何開一個worker執行緒, 呼叫Start開始不斷執行Job內容, 且不影響原本的主執行緒2. 要暫停一個執行緒不建議使用Thread.Suspend,  這會讓你不曉得在你呼叫Suspend    當下該執行緒在幹甚麼;更準確地說, 你會不曉得worker在Job中已經    完成多少(停在Job中的哪個階段)3. 不要使用Terminate, 建議使用Join3. 透過ManualResetEvent搭配WaitOne 方法, 可以讓你更精準控制: 暫停, 繼續, 以及停止執行緒

me1237guy 發表在 痞客邦 留言(2) 人氣()

image
安裝64位元版本的VTK for .NET
C:\Program Files\ActiViz.NET 5.8.0 OpenSource Edition

me1237guy 發表在 痞客邦 留言(0) 人氣()

image
include目錄設定C:\VTK 5.6\VTK\Views;C:\VTK 5.6\VTK\Charts;C:\VTK 5.6\VTK\Widgets;C:\VTK 5.6\VTK\Rendering;C:\VTK 5.6\VTK\IO;C:\VTK 5.6\VTK\Imaging;C:\VTK 5.6\VTK\Graphics;C:\VTK 5.6\VTK\Geovis;C:\VTK 5.6\VTK\GenericFiltering;C:\VTK 5.6\VTK\Filtering;C:\VTK 5.6\VTK\Common;C:\VTK 5.6\release加入下列函式庫vtkCharts.lib
vtkViews.lib
vtkWidgets.lib
vtkHybrid.lib
vtkRendering.lib
vtkGraphics.lib
vtkImaging.lib
vtkIO.lib
vtkFiltering.lib
vtkCommon.lib
vtksys.lib

me1237guy 發表在 痞客邦 留言(0) 人氣()

image

將C:\VTK 5.6\vtkdata\Data\42400-IDGH.stl加入專案
 1: // ReadSTLEx.cpp : 定義主控台應用程式的進入點。
 2: //
 3:  
 4: #include "stdafx.h"
 5: #include <vtkPolyData.h>
 6: #include <vtkSTLReader.h>
 7: #include <vtkSmartPointer.h>
 8: #include <vtkPolyDataMapper.h>
 9: #include <vtkActor.h>
 10: #include <vtkRenderWindow.h>
 11: #include <vtkRenderer.h>
 12: #include <vtkRenderWindowInteractor.h>
 13: int _tmain(int argc, _TCHAR* argv[])
 14: {
 15: std::string inputFilename = "42400-IDGH.stl";
 16: 
 17: vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New();
 18: reader->SetFileName(inputFilename.c_str());
 19: reader->Update();
 20: 
 21: // Visualize
 22: vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
 23: mapper->SetInputConnection(reader->GetOutputPort());
 24: 
 25: vtkSmartPointer<vtkActor> actor =
 26: vtkSmartPointer<vtkActor>::New();
 27: actor->SetMapper(mapper);
 28: 
 29: vtkSmartPointer<vtkRenderer> renderer =
 30: vtkSmartPointer<vtkRenderer>::New();
 31: vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
 32: renderWindow->AddRenderer(renderer);
 33: vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
 34: renderWindowInteractor->SetRenderWindow(renderWindow);
 35: 
 36: renderer->AddActor(actor);
 37: renderer->SetBackground(.0, .0, .6); // Background color green
 38: 
 39: renderWindow->Render();
 40: renderWindowInteractor->Start();
 41: return 0;
 42: }
 43:  

me1237guy 發表在 痞客邦 留言(0) 人氣()

image


 1: renWin->SetSize(300, 200);
C:\VTK 5.6\VTK\Widgets;C:\VTK 5.6\VTK\Rendering;C:\VTK 5.6\VTK\IO;C:\VTK 5.6\VTK\Imaging;C:\VTK 5.6\VTK\Graphics;C:\VTK 5.6\VTK\Geovis;C:\VTK 5.6\VTK\GenericFiltering;C:\VTK 5.6\VTK\Filtering;C:\VTK 5.6\VTK\Common;C:\VTK 5.6\release;C:\VTK 5.6\libvtkWidgets.lib
vtkHybrid.lib
vtkRendering.lib
vtkGraphics.lib
vtkImaging.lib
vtkIO.lib
vtkFiltering.lib
vtkCommon.lib
vtksys.lib

me1237guy 發表在 痞客邦 留言(0) 人氣()

image

me1237guy 發表在 痞客邦 留言(0) 人氣()

image
在進行影像處理工作時, 經常需要批次處理, 處理的對象可能是某個目錄夾下的一序列影像 {0.bmp, 1.bmp, 2.bmp,….},可以存放在一個List<string>的容器內, 紀錄該目錄夾特定附檔名的檔案名稱
 1: public static List<string> searchFileNames(string sDirPath, string fileExtension)
 2: {
 3: List<string> list = new List<string>();
 4: if (!Directory.Exists(sDirPath)) return null;
 5: foreach (string filePath in System.IO.Directory.GetFiles(sDirPath, fileExtension))
 6: {
 7: list.Add(System.IO.Path.GetFileName(filePath));
 8: }
 9: return list;
 10: }

me1237guy 發表在 痞客邦 留言(0) 人氣()

Blog Stats
⚠️

成人內容提醒

本部落格內容僅限年滿十八歲者瀏覽。
若您未滿十八歲,請立即離開。

已滿十八歲者,亦請勿將內容提供給未成年人士。